This post will help you if you need to upload multiple files at once in an MVC form. 

The examples are for an umbraco website, but the principles are the same. 

Model

Create a model

Notice the Files property is an array of HttpPostedFileBase

using System.Web;

namespace MFU.Web.Models
{
    public class UploadModel
    {
        public HttpPostedFileBase[] Files { get; set; }
    }
}

View

Create a partial view, I called mine _UploadForm

Pay attention to the enctype, if you miss this, it won't work.

@inherits Umbraco.Web.Mvc.UmbracoViewPage<UploadModel>

@using MFU.Web.Models

@using (Html.BeginUmbracoForm("SubmitForm", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="Files" id="Files" multiple />
    <button>Submit</button>
}

Call the partial from the main view

@{ Html.RenderPartial("_UploadForm", new MFU.Web.Models.UploadModel()); }

I also added this code to show the names of the files you uploaded.

@if (TempData["FileNames"] != null)
{
    <p>Last uploaded files:</p>
    @Html.Raw(TempData["FileNames"].ToString())
}

Controller

On the form submit you can iterate through the array of files and save them or do whatever you need to. In my example I am just getting the file names to show on the post back.

using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using MFU.Web.Models;
using System.Text;

namespace MFU.Web.Controllers
{
    public class UploadController : SurfaceController
    {
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(UploadModel model)
        {
            if (ModelState.IsValid)
            {
                StringBuilder fileNames = new StringBuilder();
                fileNames.Append("<ul>");
                foreach (HttpPostedFileBase file in model.Files)
                {
                    //You can do something with the files here like save them to disk or upload them into the Umbraco Media section
                    fileNames.Append("<li>");
                    fileNames.Append(file.FileName);
                    fileNames.Append("</li>");
                }
                fileNames.Append("</ul>");
                TempData["FileNames"] = fileNames.ToString();
                return RedirectToCurrentUmbracoPage();
            }
            return CurrentUmbracoPage();
        }
    }
}

Here is the GitHub repo which contains the code. 

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to use Donut Caching in Umbraco and MVC

In this post I show you how easy Donut Caching is to use in Umbraco and MVC. It's very clever and no…

Read Post

What are Content Apps in Umbraco v8

In this post I tell you about Content Apps in Umbraco v8 and show you what ideas I've had and what I…

Read Post

How to start working with Umbraco v8

In this post I give you step by step instructions for getting Umbraco v8 forked, checked out and wor…

Read Post

How to add default values to Umbraco properties in the backoffice

This post shows you how you can easily add a default value to a property when using the editor in th…

Read Post