Uploaded HttpPostedFile is null


Question

On the View:

<% =Html.BeginForm("About", "Home", FormMethod.Post, new {enctype="multipart/form-data "})%>
  <input type="file" name="postedFile" />
  <input type="submit" name="upload" value="Upload" />
<% Html.EndForm(); %>

In the Controller, there's something like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult About(HttpPostedFile postedFile)
{
    //but postedFile is null 
    View();
}

postedFile is null in About(). How do I upload the file?

1
19
12/5/2014 2:24:58 PM

Use HttpPostedFileBase (not HttpPostedFile) and also name the parameter exactly as in the form. eg. if you have

<input type="file" id="file1" name="file1" />

you have to have the method head:

public ActionResult About(HttpPostedFileBase file1)
29
12/20/2013 5:06:24 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon