Upload/ Download Files In ASP.NET Core 2.0

Problem
 
How to upload and download files in ASP.NET Core MVC.
 
Solution
 
In an empty project, update the Startup class to add services and middleware for MVC.
  1. public void ConfigureServices(  
  2.         IServiceCollection services)  
  3.     {  
  4.         services.AddSingleton<IFileProvider>(  
  5.             new PhysicalFileProvider(  
  6.                 Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));  
  7.   
  8.         services.AddMvc();  
  9.     }  
  10.   
  11.     public void Configure(  
  12.         IApplicationBuilder app,  
  13.         IHostingEnvironment env)  
  14.     {  
  15.         app.UseMvc(routes =>  
  16.         {  
  17.             routes.MapRoute(  
  18.                 name: "default",  
  19.                 template: "{controller=Home}/{action=Index}/{id?}");  
  20.         });  
  21.     }  
Add a Controller and action methods to upload and download the file.
  1. [HttpPost]  
  2.   public async Task<IActionResult> UploadFile(IFormFile file)  
  3.   {  
  4.       if (file == null || file.Length == 0)  
  5.           return Content("file not selected");  
  6.   
  7.       var path = Path.Combine(  
  8.                   Directory.GetCurrentDirectory(), "wwwroot",   
  9.                   file.GetFilename());  
  10.   
  11.       using (var stream = new FileStream(path, FileMode.Create))  
  12.       {  
  13.           await file.CopyToAsync(stream);  
  14.       }  
  15.   
  16.       return RedirectToAction("Files");  
  17.   }  
  18.   
  19.   public async Task<IActionResult> Download(string filename)  
  20.   {  
  21.       if (filename == null)  
  22.           return Content("filename not present");  
  23.   
  24.       var path = Path.Combine(  
  25.                      Directory.GetCurrentDirectory(),  
  26.                      "wwwroot", filename);  
  27.   
  28.       var memory = new MemoryStream();  
  29.       using (var stream = new FileStream(path, FileMode.Open))  
  30.       {  
  31.           await stream.CopyToAsync(memory);  
  32.       }  
  33.       memory.Position = 0;  
  34.       return File(memory, GetContentType(path), Path.GetFileName(path));  
  35.   } 
Add a Razor page with HTML form to upload a file.
  1. <form asp-controller="Home" asp-action="UploadFile" method="post"  
  2.       enctype="multipart/form-data">  
  3.       
  4.     <input type="file" name="file" />  
  5.     <button type="submit">Upload File</button>  
  6. </form> 
Discussion Uploading
 
ASP.NET Core MVC model binding provides IFormFile interface to upload one or more files. The HTML form must have the encoding type set to multipart/form-data and an input element with typeattribute set to file.
 
You could also upload multiple files by receiving a list of IFormFile in action method and setting input element with multiple attribute.
You could also have IFormFile as a property on model received by the action method.
  1. public class FileInputModel  
  2.  {  
  3.      public IFormFile FileToUpload { get; set; }  
  4.  }  
  5.  [HttpPost]  
  6.  public async Task<IActionResult> UploadFileViaModel(FileInputModel model)  
Note

The name on input elements must match action parameter name (or model property name) for model binding to work. This is no different than model binding of simple and complex types.
 
Downloading
 
Action method needs to return FileResult with either a streambyte[], or virtual path of the file. You will also need to know the content-type of the file being downloaded. Here is a sample (quick/dirty) utility method.
Source Code