Upload multiple files to MVC controller

6.7k views Asked by At

I'm trying to upload multiple image to server. HTML-

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" multiple /> 
    <input type="text" name="caption"/>
    <textarea name="description"></textarea>
    <input type="submit" value="Submit" />
</form>

I'm able to handle single file. Here is my code-

public ActionResult SubmitImage(FormCollection data)
{
     var file = Request.Files["file"];
}

How can I handle multiple files in server?

2

There are 2 answers

1
Code It On BEST ANSWER

try this-

public ActionResult SubmitImage(IEnumerable<HttpPostedFileBase> file,FormCollection data)
{
     foreach (var f in file)
     {

     }
}
0
Shashank Chaturvedi On

I believe you are misunderstanding what Request.Files contains and how to access multiple files from the Request. Here is a link that has an appropriate example for you: http://www.mikesdotnetting.com/article/125/asp-net-mvc-uploading-and-downloading-files

Hope this helps.