1

I need to upload multiple files into web server using MVC3 with RAZOR. I have the following code. In the controller, I am getting zero as the file count. How to correct it to get the actual number of files being uploaded and to get the content?

public class MyFileController : Controller
{

    public ActionResult MyFileProcessActionTest()
    {
        return View();
    }

    [HttpPost]
    public ActionResult MyFileProcessActionTest(IEnumerable<System.Web.HttpPostedFileBase> files)
    {

        int fileCount = files.Count<System.Web.HttpPostedFileBase>();
        return RedirectToAction("Index");
    }
}

VIEW

@{
ViewBag.Title = "MyFileProcessActionTest";
}

<h2>MyFileProcessActionTest</h2>

@using (Html.BeginForm())
{

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

<input type="submit"  />

}

READING:

  1. Binding HttpPostedFileBase using Ajax.BeginForm

  2. ASP.NET MVC Uploading and Downloading Files http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files

  3. How do I Validate the File Type of a File Upload?

  4. MVC 3 file upload and model binding

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

8

You must include the enctype attribute in the form tag to indicate that the form should include files.

@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new {enctype="multipart/form-data"))
{
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    Very often people miss enctype="multipart/form-data" for forms that contains file upload – linquize Feb 23 '12 at 10:59
  • 1
    i believe we can't post files using `GET` method either – Muhammad Adeel Zahid Feb 23 '12 at 11:02
  • I am getting `file` as `null` and `Request.Files.Count` is 0 too, would there be any difference if the `form` is an `AjaxForm` and there are `routeValues` as well? – bjan May 30 '12 at 10:59
  • @bjan You cannot upload file that way with ajax... you should search on google How to upload a file with ajax using an iframe – Bellash Feb 27 '14 at 16:10
6

change your form to match the following

@using(Html.BeginForm("action","controller",FormMethod.Post,new{encType = "multipart/form-data"})){
{

<input type="file" name="files[0]" id="file1" />
<input type="file" name="files[1]" id="file2" />
<input type="file" name="files[2]" id="file3" />

<input type="submit"  />

}

indices 0,1,2 will allow modelbinder to bind to IEnumerable furthermore encType also has to be specified when posting files to the server

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • Thanks. Worked. @using (Html.BeginForm("MyFileProcessActionTest", "MyFile", FormMethod.Post, new { enctype = "multipart/form-data" })) { – LCJ Feb 23 '12 at 11:17
  • 1
    I am getting `file` as `null` and `Request.Files.Count` is 0 too, would there be any difference if the `form` is an `AjaxForm` and there are `routeValues` as well? – bjan May 30 '12 at 10:59
  • bjan, I have not tested it with ajax, but i believe it should work even with ajax. – Muhammad Adeel Zahid May 30 '12 at 13:25
  • @bjan You cannot upload file that way with ajax... you should search on google `How to upload a file with ajax using an iframe` – Bellash Feb 27 '14 at 16:09