Introduction
We are going to discuss single and multiple file uploads with the help of the IFormFile Interface and others that are provided by .NET and step-b. y-step i.ag .NET Core 6 Web API.
- .NET provides an IFormFile interface that represents transmitted files in an HTTP request.
- Also, it provides many properties like ContentDisposition, ContentType, FileName, Headers, Name, and Length.
- IFormFile also provides many methods, like copying the request stream content, opening the request stream for reading, and many more.
Prerequisites
- .NET Core 6 SDK
- Visual Studio 2022
- SQL Server
- Postman
Step By Step Implementation
Step 1. Create a new .NET Core Web API.

Step 2. Install the following NuGet Packages.

Step 3. Create the following file entities.
FileDetails.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FileUpload.Entities
{
[Table("FileDetails")]
public class FileDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string FileName { get; set; }
public byte[] FileData { get; set; }
public FileType FileType { get; set; }
}
}
FileUploadModel
namespace FileUpload.Entities
{
public class FileUploadModel
{
public IFormFile FileDetails { get; set; }
public FileType FileType { get; set; }
}
}
FileType
namespace FileUpload.Entities
{
public enum FileType
{
PDF = 1,
DOCX = 2
}
}
Step 4. Next, the DbContextClass.cs class inside the Data folder.
using FileUpload.Entities;
using Microsoft.EntityFrameworkCore;
namespace FileUpload.Data
{
public class DbContextClass : DbContext
{
protected readonly IConfiguration Configuration;
public DbContextClass(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}
public DbSet<FileDetails> FileDetails { get; set; }
}
}
Step 5. Create IFileService and FileService files.
IFileService
using FileUpload.Entities;
namespace FileUpload.Services
{
public interface IFileService
{
public Task PostFileAsync(IFormFile fileData, FileType fileType);
public Task PostMultiFileAsync(List<FileUploadModel> fileData);
public Task DownloadFileById(int fileName);
}
}
FileService
using FileUpload.Data;
using FileUpload.Entities;
using Microsoft.EntityFrameworkCore;
namespace FileUpload.Services
{
public class FileService : IFileService
{
private readonly DbContextClass dbContextClass;
public FileService(DbContextClass dbContextClass)
{
this.dbContextClass = dbContextClass;
}
public async Task PostFileAsync(IFormFile fileData, FileType fileType)
{
try
{
var fileDetails = new FileDetails()
{
ID = 0,
FileName = fileData.FileName,
FileType = fileType,
};
using (var stream = new MemoryStream())
{
fileData.CopyTo(stream);
fileDetails.FileData = stream.ToArray();
}
var result = dbContextClass.FileDetails.Add(fileDetails);
await dbContextClass.SaveChangesAsync();
}
catch (Exception)
{
throw;
}
}
public async Task PostMultiFileAsync(List<FileUploadModel> fileData)
{
try
{
foreach(FileUploadModel file in fileData)
{
var fileDetails = new FileDetails()
{
ID = 0,
FileName = file.FileDetails.FileName,
FileType = file.FileType,
};
using (var stream = new MemoryStream())
{
file.FileDetails.CopyTo(stream);
fileDetails.FileData = stream.ToArray();
}
var result = dbContextClass.FileDetails.Add(fileDetails);
}
await dbContextClass.SaveChangesAsync();
}
catch (Exception)
{
throw;
}
}
public async Task DownloadFileById(int Id)
{
try
{
var file = dbContextClass.FileDetails.Where(x => x.ID == Id).FirstOrDefaultAsync();
var content = new System.IO.MemoryStream(file.Result.FileData);
var path = Path.Combine(
Directory.GetCurrentDirectory(), "FileDownloaded",
file.Result.FileName);
await CopyStream(content, path);
}
catch (Exception)
{
throw;
}
}
public async Task CopyStream(Stream stream, string downloadPath)
{
using (var fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write))
{
await stream.CopyToAsync(fileStream);
}
}
}
}






Jose AlcantaraPosted Jun 16, 2023, 1:05 PM
Thank you, your article has been very helpful for me, much better than .net documentation.
Maciej MrozPosted May 7, 2023, 12:40 PM
Is there any update about multiple files
Sujoy RoojPosted Apr 24, 2023, 6:29 PM
How to connect this with a angular app , can you please help me with that
nermin tanovicPosted Nov 30, 2022, 7:51 PM
Another question? do you know how to consume the UploadSingleFile service via javascript?
nermin tanovicPosted Nov 30, 2022, 7:49 PM
Very nice article. thank you. I noticed upload multiple files is not working. are you aware? or perhaps I do not know how to use swagger for this