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. 

Thank you

If you would like to say thank you or support me, then please become a Patron on my Patreon Page. You will get stickers if you join on the $5 rate.

Sponsor this website

If you would like to sponsor this website you can do that here

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

5 things a developer should do before asking for help

In this post I give you my opinions and experience about when I think you should ask for help and wi…

Read Post

Some questions answered about Umbraco Cloud

In this post I give you some answers to the questions I had about Umbraco Cloud at the beginning of…

Read Post

How to solve the Web API error 405 Method Not Allowed

This post shows you how to solve this 405 error and get it working properly.

Read Post

10 Web Developer tips I learned at Codegarden 2018

In this post I share with you 10 web developer tips and tricks I learned at Codegarden 2018

Read Post