multiple files upload using single file tag

2019-02-28 17:30发布

问题:

I Have seen many posts about everyone is using this approach

<form action="" method="post" enctype="multipart/form-data">    
  <input type="file" name="files" id="file1" />
  <input type="file" name="files" id="file2" />    
  <input type="submit"  />
</form>

And then in controller the use

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    foreach(var file in files)
    {
            file.SaveAs("myPath");
    }     
    return RedirectToAction("Index");
}

I tried and succeeded this way. But when I do it with

<form action="" method="post" enctype="multipart/form-data">    
  <input type="file" name="files" id="file" multiple />   
  <input type="submit"  />
</form>

This selects multiple files at the client end but If I use the same above(controller's) code in controller., I am able to upload only one file.

What cud be the solution (the controller code) If I want to facilitate user to select all desired image once (suppose he/has them in same folder/directory) means using single file tag. Unlike (these many articles)

http://www.codeproject.com/Articles/442515/Uploading-and-returning-files-in-ASP-NET-MVC

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/

and http://demos.devexpress.com/MVCxFileManagerAndUploadDemos/UploadControl/MultiFileUpload

回答1:

[HttpPost]
public ActionResult Upload()
{
foreach(var file in Request.Files)
{
        file.SaveAs("myPath");
}     
return RedirectToAction("Index");
}