6

Can I use:

<input type="file" name="files" id="files" multiple="multiple" />

and bind it to:

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    ...
}

I'm writing a web app for modern browsers and don't have to worry about IE so I'd like to avoid using Flash. Right now files is always null when I post the form. Is there any way to get this to work in MVC 3?

Thanks!

3
  • 1
    possible duplicate of Multi-File Upload with HTML 5 Commented Jan 9, 2012 at 0:23
  • 1
    You will probably need to create a custom model binder. Commented Jan 9, 2012 at 0:28
  • Or make the signature public ActionResult Upload(HttpPostedFileBase[] files) Commented Jan 9, 2012 at 0:31

4 Answers 4

13

Do you have your encoding set correctly in your form?

I believe you still need:

new { enctype = "multipart/form-data" }

In the form declaration to ensure the browser can post files.

For example:

@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
Sign up to request clarification or add additional context in comments.

1 Comment

OK, spoke too soon. Went back to double check and I had a type-o there that was preventing the file from posting!
1

Wouldn't one use the Request.Files for backward compatibility as follows:

public ActionResult UploadFiles()
{
  string UpoadedFilesFolder = "YourServerFolder";
  string fileName ="";
  byte[] fileData=null;
  foreach (HttpPostedFileBase uf in Request.Files)
  {
    HttpPostedFileBase UpoadedFile = uf;
    if (uf.ContentLength > 0)
    {
      fileName = Path.GetFileName(UpoadedFile.FileName);
      using (BinaryReader br = new BinaryReader(UpoadedFile.InputStream))
      {
        fileData = br.ReadBytes((int)UpoadedFile.InputStream.Length);
      }
      using (FileStream fs = new FileStream(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(UpoadedFilesFolder), fi.FileName), FileMode.Create))
      {
        fs.Write(fileData, 0, fileData.Length);
      }
    }
  }
  return Content("OK");
}

Comments

0

My Index View:

    @using (Html.BeginForm("Upload","home", FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" value=" " multiple="multiple" />
    <input type="submit" name="btUpload" value="Upload" />
}   

In controller

public ActionResult Upload(HttpPostedFileBase[] files)
        {
            TempData["Message"] = files.Count();
            return RedirectToAction("Index");
        }   

And files contains uploaded files - works fine for me!

Comments

0

This won't work:

foreach (HttpPostedFileBase uf in Request.Files)
{
    HttpPostedFileBase UpoadedFile = uf;
}

Should do like this:

for (int i=0; i<Request.Files.Count; i++)
{
  HttpPostedFileBase UpoadedFile = Request.Files[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.