I have an ASP.NET MVC 3 application. I need to implement a file uploader action within it. For some reason, when I post my form, the Request.Files collection is empty. I have been able to confirm this by setting a breakpoint. So I know that I'm reaching the action. However, I can't figure out why the Request.Files collection is empty. Here are my relevant HTML, AreaRegistration, and Controller snippets.
index.html
<form action="/files/upload/uniqueID" method="post" enctype="multipart/form-data">
<div>Please choose a file to upload.</div>
<div><input id="fileUpload" type="file" /></div>
<div><input type="submit" value="upload" /></div>
</form>
MyAreaRegistration.cs
context.MapRoute(
"FileUpload",
"files/upload",
new { action = "UploadFile", controller = "Uploader" }
);
UploaderController.cs
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFile(int uniqueID)
{
foreach (string file in Request.Files)
{
// I never get here :(
}
return View();
}
I have not made any changes to the default web.config file. Is there some setting I need to add? I can't figure out why the Request.Files collection would be empty. Can someone please help me?
Thank you so much!
You should use HttpPostedFileBase for you controller and do something like that
And for the view
Check Phil Haack blog here for this problem: Uploading a File (Or Files) With ASP.NET MVC
I believe the problem is with your
action
attribute in your<form />
tag:I think upon post it is trying to pass the string
"uniqueID"
to your Action method. When you hit your breakpoint, what is the value of youruniqueID
parameter set to when you reach the action methodUploadFile()
?Use the
HtmlHelper.BeginForm()
method to use Razor to construct the form.