I need some help. I’m trying to upload files using <input type="file">. Here is my View:
@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="files[0]" id="files[0]" />
<input type="file" name="files[1]" id="files[1]" />
<input type="submit" value="Upload Book" />
}
And here is an Action which should process uploaded file.
[HttpPost]
public ActionResult BookAdd(IEnumerable<HttpPostedFileBase> files)
{
// some actions
return View();
}
The problem is that “files” always contains two elements which are null.
What can be done to fix it?
It’s time for some news. It seems like I found the problem, but I still don’t know how to fix it. It appears that despite the fact I’m using “multipart/form-data” here:
@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input type="file" name="File" id="file1" />
<input type="file" name="File" id="file2" />
<input type="submit" value="Upload Book" />
}
Request.ContentType remains “application/x-www-forum-urlencoded” in controller..
Just get rid of the square brackets in the names of your input fields:
UPDATE:
After looking at the sample project you sent me the problem is that you have 2 nested forms. This is not allowed in HTML. You have one form in your
_Layout.cshtmland another form in yourBookAdd.cshtmlview. That’s the reason why despite theenctype="multipart/form-data"attribute on your inner form you were getting the wrongRequest.ContentType. So you will have to unnest those forms if you want this to work. Also in the example you sent me yourBookAddcontroller action doesn’t have the correct signature taking a list of files, but I guess that’s due to some tests you were doing.