in this session, we will discuss how to upload multiple files in asp.net core with file upload controller step by step
Create ASP.NET Core Project: open Visual Studio, create a new ASP.NET Core project

Select Empty Template on next screen and Click Ok button to Finish



Add Configurations: Open Startup.cs file in your project and add new configurations as given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace SampleHtml
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Product}/{action=Index}/{id?}");
});
}
}
}
UseStaticFiles(): In asp.net core by default directory for store static files (CSS files, js files, images )in wwwroot and default asp.net core applications will not serve(accessing ) static file for that we will use UsestaticFiles()middleware is required and for accessing default file(HTML files) we need UseDefaultFile() middleware is required Readmore about asp.net core middlewares.
Add a business model class(Entities Class): Create a new folder named as Models. In the Models folder, you will create new entities class as below(name as a like product):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SampleHtml.Models
{
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public List<string> Photos { get; set; }
}
}
Add Controller: Create a new folder named Controllers in your project. In this folder, create a new controller named ProductController.cs as given below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SampleHtml.Models;
namespace SampleHtml.controlles
{
[Route("Product")]
public class ProductController : Controller
{
[Route("")]
[Route("index")]
[Route("~/")]
public IActionResult Index()
{
return View("Index", new Product());
}
private IHostingEnvironment ihostingEnvironment;
public ProductController(IHostingEnvironment _ihostingEnvironment)
{
this.ihostingEnvironment = _ihostingEnvironment;
}
[Route("save")]
[HttpPost]
public IActionResult Save(Product product, IFormFile[] photos)
{
if (photos == null || photos.Length == 0)
{
return Content("File(s) not selected");
}
else
{
product.Photos = new List<string>();
foreach (IFormFile photo in photos)
{
var path = Path.Combine(this.ihostingEnvironment.WebRootPath, "images", photo.FileName);
var stream = new FileStream(path, FileMode.Create);
photo.CopyToAsync(stream);
product.Photos.Add(photo.FileName);
}
}
ViewBag.product = product;
return View("Success");
}
}
}
Add View: Add a new folder named Views. In this folder, add a new folder named Product and add new views as given below:
@model SampleHtml.Models.Product
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://tutorialshelper.com/demo/Css/dynamictable.css">
</head>
<body>
<h1>How to upload multiple files in asp.net core</h1>
<form asp-controller="product" asp-action="save" method="post" enctype="multipart/form-data">
<table class="table blueTable" cellpadding="2" cellspacing="2">
<tr>
<td>Id</td>
<td>
<input type="text" asp-for="Id" />
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" asp-for="Name" />
</td>
</tr>
<tr>
<td>Price</td>
<td>
<input type="text" asp-for="Price" />
</td>
</tr>
<tr>
<td>Photo</td>
<td>
<input type="file" name="photos" multiple="multiple" />
</td>
</tr>
<tr>
<td>Â </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</form>
</body>
</html>
Add Success Page: In Product folder, create a new view name as like Success.cshtml as given below:
@page
@model SampleHtml.Views.Product.SuccessModel
@{
}
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Success</title>
<link rel="stylesheet" href="https://tutorialshelper.com/demo/Css/dynamictable.css">
</head>
<body>
<h1>How to upload multiple files in asp.net core</h1>
<table class="table blueTable" cellpadding="2" cellspacing="2" border="1">
<tr>
<td>Id</td>
<td>@ViewBag.product.Id</td>
</tr>
<tr>
<td>Name</td>
<td>@ViewBag.product.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@ViewBag.product.Price</td>
</tr>
<tr>
<td>Photo(s)</td>
<td>
@foreach (var photo in ViewBag.product.Photos)
{
<img src="~/images/@photo" width="120" />
}
</td>
</tr>
</table>
</body>
</html>
Add _ViewImports.cshtml page: Select Razor View Imports item and click Add button to Finish and Add Inside _ViewImports.cshtml file and TagHelpers library as given below:


_ViewImports.cshtml page:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Structure of your project look like given below:


Run Your project: you will Access Product action in Production controller with the following URL: http://localhost:44370/Demo/Index see the given below output




Files upload in asp.net core(Single files)
the above article we are discussed How to upload multiple files in asp.net core right now we will discuss How to upload single files in asp.net core
Add Controller: Go to controllers folder add controller name like production.cs add the following code
[Route("product")]
public class ProductController : Controller
{
private IHostingEnvironment ihostingEnvironment;
public ProductController(IHostingEnvironment _ihostingEnvironment)
{
this.ihostingEnvironment = _ihostingEnvironment;
}
[Route("")]
[Route("index")]
[Route("~/")]
public IActionResult Index()
{
return View("Index", new Product());
}
[Route("save")]
[HttpPost]
public IActionResult Save(Product product, IFormFile photo)
{
if (photo == null || photo.Length == 0)
{
return Content("File not selected");
}
else
{
var path = Path.Combine(this.ihostingEnvironment.WebRootPath, "images", photo.FileName);
var stream = new FileStream(path, FileMode.Create);
photo.CopyToAsync(stream);
product.Photo = photo.FileName;
}
ViewBag.product = product;
return View("Success");
}
Index View: Go to views folder add index.cshtml page
@{
Layout = null;
}
@model SampleHtml.Models.Product.Models.Product
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-controller="product" asp-action="save" method="post" enctype="multipart/form-data">
<table cellpadding="2" cellspacing="2">
<tr>
<td>Id</td>
<td>
<input type="text" asp-for="Id" />
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" asp-for="Name" />
</td>
</tr>
<tr>
<td>Price</td>
<td>
<input type="text" asp-for="Price" />
</td>
</tr>
<tr>
<td>Photo</td>
<td>
<input type="file" name="photo" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</form>
</body>
</html>
try to run your application


thanks
- Crud operation in asp.net core using entity framework core code first - May 22, 2020
- How to handle sessions in asp.net core with example - April 30, 2020
- How to upload multiple files in asp.net core - April 29, 2020
how can do, to save with a new name the files
thanks for visiting, changing file name you have to change manually name or implement at server-side code