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

2 Answers 2

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(IEnumerable<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"  />
Sign up to request clarification or add additional context in comments.

1 Comment

but i want to upload files using a single input field, is that any way in c#
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.

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.