Monday, March 19, 2012

Multiple file upload with MVC 3

   1: public ActionResult Upload()
2: {
3: return View();
4: }
5:
6: [HttpPost]
7: public ActionResult Upload(IEnumerable<HttpPostedFileBase> fileUpload)
8: {
9: foreach (var file in fileUpload)
10: {
11: if (file.ContentLength > 0)
12: {
13: var fileName = Path.GetFileName(file.FileName);
14: var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
15: file.SaveAs(path);
16: }
17: }
18: return RedirectToAction("Index");
19: }
Microsoft Web Helper is very handy to use. Above example is shown how to use MS Web helper's Multiple file upload.

See below how the ui should be implement for Muliple file upload.



 <h2>Upload</h2>       
@FileUpload.GetHtml(5, uploadText: "Upload files")

No comments: