In this article I will explain a simple Tutorial with example, how to upload multiple files using HTML5 in ASP.Net MVC 5.
HTML5 has provided an additional feature to select and upload multiple files and the same will be used along with ASP.Net MVC.
 
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
View
The following View consists of an HTML FileUpload element, a Submit Button and a SPAN element for displaying Message enclosed in a Form element.
The HTML FileUpload element has been specified with an additional HTML5 attribute multiple = “multiple” in order to allow user to select multiple files.
The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.
ActionNameName of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <span>Select File:</span>
            <input type="file" name="postedFiles" multiple="multiple"/>
            <hr/>
            <input type="submit" value="Upload"/>
            <br/>
            <span style="color:green">@Html.Raw(ViewBag.Message)</span>
        }
    </div>
</body>
</html>
 
 
Namespaces
You will need to import the following namespace.
using System.IO;
 
 
Controller
The Action method Index by default supports the GET operation and hence another overridden method for POST operation is created which accepts the parameter which is a collection of type HttpPostedFileBase.
Note: The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same, otherwise the HttpPostedFileBase parameter will be NULL.
 
First a check is performed whether Directory (Folder) exists if not then the Directory (Folder) is created and then a loop is executed and one by one each file is saved to the Directory (Folder).
Finally a message is displayed to the user using ViewBag. For more details on displaying message using ViewBag, please refer my article Display (Pass) String Message from Controller to View in ASP.Net MVC.
public class HomeController : Controller
{
    // GET: Home
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(List<HttpPostedFileBase> postedFiles)
    {
        string path = Server.MapPath("~/Uploads/");
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
 
        foreach (HttpPostedFileBase postedFile in postedFiles)
        {
            if (postedFile != null)
            {
                string fileName = Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(path + fileName);
                ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
            }
        }
 
        return View();
    }
}
 
 
Screenshot
Upload multiple files using HTML5 in ASP.Net MVC
 
 
Downloads
Download Code