Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC: upload multiple image files?

Tags:

is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?

I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.

Thanks

like image 315
urlreader Avatar asked Sep 10 '14 18:09

urlreader


People also ask

How to use multiple file upload in ASP net MVC?

Add more <input type="file" /> and use Request. Files to get an enumerable of HttpPostedFileBase on your controller's action. Then skip adding more inputs and use Request. Files on your action.

What in ASP.NET is a collection of single file or multiple files?

ASP.NET caches all data in server memory or to disk depending on the uploaded file size. ASP.NET MVC defines the controller and appropriate action method that will handle the request. The action method handles the request (for example, saves files on a hard disk, or updates a database, etc.)


1 Answers

You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:

[HttpPost] public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)  {     foreach (var file in files)     {         file.SaveAs(Server.MapPath("~/Update/" + file.FileName));     }      return View(); } 

Alternatively, you could read Request.Files and do the same job,

[HttpPost] public ActionResult Upload()  {     foreach (var file in Request.Files)     {         file.SaveAs(Server.MapPath("~/Update/" + file.FileName));     }      return View(); } 

See Also

  • Single File Upload to Multiple File Upload in MVC
  • Uploading a File (Or Files) With ASP.NET MVC
like image 96
Felipe Oriani Avatar answered Sep 18 '22 03:09

Felipe Oriani