Download file Sample Project
Uploading several files is nice and clean with the help of the FileCollectionModelBinder available in the ASP.NET MVC futures assembly. Please follow the procedure below to create a sample.
<form method="post" enctype="multipart/form-data" action="/home/uploadfiles"> <div> <input type="file" name="fileName" id="fileName" accept="file/zip" /> <input type="file" name="fileName" id="file3" accept="file/zip" /> <%=Html.ValidationMessage("fileName") %> </div> <div> <input type="submit" value="Upload file"/> </div> </form>
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); FileCollectionModelBinder.RegisterBinder(ModelBinders.Binders); }
[AcceptVerbs(HttpVerbs.Post)] public ActionResult UploadImage(HttpPostedFileBase[] files) { // process files here }
A complete sample is available for download.
In order to upload one file at a time, you do not need the futures assembly. The built-in HttpPostedFileBaseModelBinder can handle this.