Hi,
I have googled this code to upload a file with MVC.
<form method="post" enctype="multipart/form-data" action="/Task/SaveFile">
<input type="file" id="FileBlob" name="FileBlob"/>
<input type="submit" value="Save"/>
<input type="button" value="Cancel" onclick="window.location.href='/'" />
</form>
But when interrogate the forms["FileBlob"] it is null when I browse a file and submit the form????
Malcolm
EDIT: I have added a textbox to the form and I can get that value fine. Just the input type file is not working?
bool errors = false;
//this field is never empty, it contains the selected filename
if ( string.IsNullOrEmpty( forms["FileBlob"] ) )
{
errors = true;
ModelState.AddModelError( "FileBlob", "Please upload a file" );
}
else
{
string sFileName = forms["FileBlob"];
var file = Request.Files["FileBlob"];
//'file' is always null, and Request.Files.Count is always 0 ???
if ( file != null )
{
byte[] buf = new byte[file.ContentLength];
file.InputStream.Read( buf, 0, file.ContentLength );
//do stuff with the bytes
}
else
{
errors = true;
ModelState.AddModelError( "FileBlob", "Please upload a file" );
}
}
if ( errors )
{
return ShowTheFormAgainResult();
}
else
{
return View();
}
}