File upload functionality is one of the most common requirements in modern web applications. Whether users are uploading profile pictures, documents, PDFs, Excel files, or images, handling file uploads securely and efficiently is essential.
ASP.NET Core provides built-in support for file uploads using the IFormFile interface. In this article, we will learn how to upload files in ASP.NET Core step by step, including how to save them to the server safely.
Why File Upload Is Important
File uploading is widely used in:
Profile image uploads
Resume submission forms
Document management systems
Invoice and report uploads
E-learning platforms
Admin dashboards
Because it is used in almost every business application, understanding file upload in ASP.NET Core is a must for developers.
Understanding I form file in ASP.NET Core
In ASP.NET Core, uploaded files are represented using the IFormFile interface. This interface provides access to:
File name
File size
File content type
File content stream
When a form is submitted with enctype="multipart/form-data", ASP.NET Core automatically binds the uploaded file to an IFormFile parameter.
Method 1: Basic File Upload and Save to Server
Let’s create a simple API endpoint that allows users to upload a file and save it inside a folder on the server.
Controller Code
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class FileUploadController : ControllerBase
{
[HttpPost("upload")]
public async Task<IActionResult> UploadFile(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("No file selected.");
var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Uploads");
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
var filePath = Path.Combine(folderPath, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok("File uploaded successfully.");
}
}How This Works
The file is received as an IFormFile parameter.
We check whether the file exists.
We create an Uploads folder if it does not exist.
The file is saved using FileStream.
A success message is returned.
This is the simplest and most common way to upload files in ASP.NET Core.
Method 2: Secure File Upload with Validation (Recommended)
In real-world applications, simply saving files is not enough. We must validate:
File size
File type
Duplicate file names
Security risks
Marcin Szolke ScholkePosted Feb 26, 2026, 5:55 PM
20 years ago i know i had problem with this using php :D