I have this form in my opinion:
<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>
And this code in my controller:
public ActionResult SaveFile( FormCollection forms ) { 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(); } }
Based on each sample code I could find, this seems like a way to do this. I tried with small and large files, without any difference in the result. The form field always contains the file name that matches my choice, and the Request.Files collection is always empty.
I do not think this is relevant, but I am using VS Development Web Server. AFAIK supports downloading files in the same way as IIS.
It is getting late, and there is a chance that I will miss something obvious. I would appreciate any advice.
upload asp.net-mvc
Jason Diller Nov 18 '08 at 5:53 2008-11-18 05:53
source share