Upload file in ASP.NET MVC (Again!)

4.4k Views Asked by At

I have a problem with uploading file in asp.net mvc 2. My controller function's parameter is a FormCollection type. Because the fields are too numerous, I can't separate each field as a parameter. I have 2 upload file fields in my form. How can I get uploaded files in my controller?

I tried this way:

public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
    ...
}

but personImage was null. :(

or this way:

HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];

but img was null to. Also collection["personImage"] was the name of selected file (without path) and I can't cast it to HttpPostedFileBase.

Note that all fields must be filled in on one page. I can't let the customer upload images in a separate page!

2

There are 2 best solutions below

2
On BEST ANSWER

Start by reading this blog post. Then apply it to your scenario:

<form action="/Home/CreateAgent" method="post" enctype="multipart/form-data">

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

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
</form>

which translated in WebForms language gives:

<% using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
    <input type="file" name="file1" id="file" />
    <input type="file" name="file2" id="file" />

    ... Some other input fields for which we don't care at the moment
        and for which you definetely should create a view model
        instead of using FormCollection in your controller action

    <input type="submit" />
<% } %>

and then:

public ActionResult CreateAgent(
    // TODO: To be replaced by a strongly typed view model as the 
    // ugliness of FormCollection is indescribable
    FormCollection collection, 
    HttpPostedFileBase file1, 
    HttpPostedFileBase file2
)
{
    // use file1 and file2 here which are the names of the corresponding
    // form input fields
}

If you have many files then use IEnumerable<HttpPostedFileBase> as illustrated by Haacked.

Remarks:

  • Absolutely never use this.HttpContext.Request.Files in an ASP.NET MVC application
  • Absolutely never never never use this.HttpContext.Request.Files[collection["personImage"]] in an ASP.NET MVC application.
2
On

What does your using statement look like in your view for the form? It should look something like this:

using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })