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
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.
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.)
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With