File Upload in ASP.NET 5 vNext and MVC 6

Introduction

In MVC 6 beta 3 version of ASP.NET 5 vNext, uploading and saving file’s become very easy. In other words, ASP.NET 5 vNext now support model binding of multipart form data. So, now we can easily upload and save files.  In this article, I will show you a sample way to how to upload and save file’s in ASP.NET 5 vNext.

Description

Let’s assuming that you are running Visual Studio 2015 CTP 6 or later and you have created an ASP.NET 5 vNext web application. Add a cshtml form and input type=form element in your mvc view as below.

<form method="post" enctype="multipart/form-data>
     <input type="file" name="files" id="files" multiple />
     <input type="submit" value="submit"/>
</form>

Then inside your controller add these lines.


public class HomeController : Controller
{
 IApplicationEnvironment hostingEnvironment;
 public HomeController(IApplicationEnvironment _hostingEnvironment)
 {
   hostingEnvironment = _hostingEnvironment;
 }
 [HttpGet]
 public IActionResult Index()
 {
   return View();
 }

 [HttpPost]
 public IActionResult Index(IList<IFormFile> files)
 {
      foreach (var file in files)
      {
          var fileName = ContentDispositionHeaderValue
            .Parse(file.ContentDisposition)
            .FileName
            .Trim('"');
            var filePath = hostingEnvironment.ApplicationBasePath + "\\Documents\\" + DateTime.Now.ToString("yyyyddMHHmmss") + fileName;
             file.SaveAs(filePath);
      }
      return Index();
  }
}

In the Index post method constructor, I am passing IList<IFormFile> files as a parameter which will be model bind by the latest framework. Then for each file in files I am getting file name by parsing file.ContentDisposition using ContentDispositionHeaderValue parse method. Then I am saving the file using IFormFile.SaveAs method.

Summary

ASP.NET 5 vNext MVC 6 beta 3 makes it very easy to upload and save files. In this article, I showed you, how to upload and save file’s in ASP.NET 5 vNext. You can download code from here Sample file upload in mvc 6 with aspnet vnext

3 thoughts on “File Upload in ASP.NET 5 vNext and MVC 6

  1. what if you are posting the file with other fields on the form? Don’t think IList will cut that…My old asp.net had the file field in the form with other fields. Had to use MultipartFormDataStreamProvider but don’t see that anymore. Thanks.

    Like

  2. hi!,I like your writing very so much! proportion we be in contact more approximately your post on AOL? I need an expert in this house to unravel my problem. May be that’s you! Taking a look ahead to peer you.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s