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!
Start by reading this blog post. Then apply it to your scenario:
which translated in WebForms language gives:
and then:
If you have many files then use
IEnumerable<HttpPostedFileBase>as illustrated by Haacked.Remarks:
this.HttpContext.Request.Filesin an ASP.NET MVC applicationthis.HttpContext.Request.Files[collection["personImage"]]in an ASP.NET MVC application.