0

I'm attempting upload multiple files in ASP.NET MVC and my controller is

 public ActionResult GalleryAdd()
    {
        foreach (string fil in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[fil];
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Images/Gallery"), fileName);
            file.SaveAs(path);
        }
        return RedirectToAction("Index");
    }

And my input field is

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

Problem is that always upload only one file(first file) . Foreach loop only take the first file , but Request.Files Count shows number of file uploaded. What is the problem here

neel
  • 5,123
  • 12
  • 47
  • 67

2 Answers2

2

Change the signature of your GalleryAdd action to take an IEnumerable of HttpPostedFileBase, then you can iterate over the files passed in from the view:

public ActionResult GalleryAdd(IEnumberable<HttpPostedFileBase> files)
{
     foreach (string file in files)
     {
         //iterate over files
     }
}

Then add a file input for each file to add:

<form action="@Url.Action(GalleryAdd)" method="post" enctype="multipart/form-data">

    <label for="file1">Filename:</label>
    <input type="file" name="files" id="file1" />

    <label for="file2">Filename:</label>
    <input type="file" name="files" id="file2" />

    <input type="submit"  />

James
  • 2,195
  • 1
  • 19
  • 22
0

you can write webservice that will be called in controller. use this link for getting help regarding upload file also look in this link.

Community
  • 1
  • 1
manishkr1608
  • 313
  • 1
  • 3
  • 13