I have used asp.net mvc since beta and uploaded files without any problems until yesterday.
I kept getting null from Request.Files (Actually I use HttpPostedFile as inparameter to the action). Nevertheless both Request.Files and the inparamer was null.
In the form:
<% using (Html.BeginForm("UploadFile", "Product", FormMethod.Post,
new {enctype = "multipart/form-data"})) {%>
<%} %>
In the controller:
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/upload"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Edit", "Product");
}
Can you spot the error? Well it took me quite some time to figure it out:
The <input type="file" id="file" /> needs to hade a name. Changing it to <input type="file" id="file" name=file" /> solves the problem.
Comments are closed.