Uploading multiple files with ASP.NET MVC

4.41 (29 votes)

I have looked at uploading individual files and downloading multiple files in MVC, and I've had a few requests recently asking me to complete the series by showing how to upload multiple files in an ASP.NET MVC application.

When designing the form for uploading, you can choose from a number of approaches. You can use plain HTML elements. If you know that your users are using browsers that implement the HTML5 file API, you can provide a single file input with the multiple attribute:

<input type="file" name="files" multiple />

This enables multiple file selection, but is not available in Internet Explorer 9 or below. It is available in most other modern browsers. One alternative is to provide a number of inputs for users of IE9 or lower. You can selectively choose to display the single input with the multiple attribute set or multiple individual inputs based on the features supported by the browser. Here's the HTML part, including the form declaration which requires the method to be set to POST and an enctype set to multipart/form-data for file uploading to work:

@using (Html.BeginForm("Multiple", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    <div id="multiple">
        <input type="file" class="multiple" name="files" multiple />
    <div id="single">
        <input type="file" class="single" name="files" /><br />
        <input type="file" class="single" name="files" /><br />
        <input type="file" class="single" name="files" /><br />
    <button class="btn btn-default">Upload</button>   

And here's the jquery code that switches between the two options:

@section Scripts{
    <script type="text/javascript">
        $(function () {
            if(window.FileReader != null){
                $('.single').prop('disabled', true);
            } else {
                $('.multiple').prop('disabled', true);

Browsers that implement the HTML5 file API include a component called Filereader. If this is null, the browser doesn't provide the required support and the input with the multiple attribute is disabled and hidden.

Files are uploaded to the server in the Request.Files collection, each one of which is mapped to an HttpPostedFileBase object by the default MVC model binder. Therefore, the controller action that is tasked to process the uploaded files needs a parameter that represents a collection of HttpPostedFileBase objects. This can be a property on a model, or a simple List<HttpPosteFileBase>parameter. The following action takes each file and saves it with a new name generated from a Guid to a folder called "uploads":

public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
    foreach (var file in files)
        if (file != null && file.ContentLength > 0)
            file.SaveAs(Path.Combine(Server.MapPath("/uploads"), Guid.NewGuid() + Path.GetExtension(file.FileName)));
    return View();

An HttpPostedFileBase collection is created and sized to the number of uploads on a form regardless whether the user populated it, or the number of files uploaded if the multiple attribute approach is used. So when testing to see if a file has been uploaded, you should check to see if it is null before testing to see if the ContentLength is greater than 0. If you prefer to save the files in a database, either as binary data or pointers to files in the file system you can read how to do that from my previous article on file uploading: ASP.NET MVC 5 with EF 6 - Working With Files.


This brief article shows how to upload multiple files in an ASP.NET MVC application by using only HTML solutions (no Flash). It describes the multiple attribute available as part of HTML5 and shows how to fallback to an alternative solution in the cases where the browser lacks the required support.

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 64708

1 Comment

- Manoj Kulkarni

Nice article. Thank you for sharing.

Recent Comments

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...