File upload in asp.net core web API is as easy as WCF and standard ASP.NET API. Here this blog post shares some code examples which will help to explore in detail to upload a file in asp.net core 6 web API using IFormFile interface.
Upload File ASP.NET Core Web API using IFormFile
IFormFile
IFormFile interface is derived from Microsoft.AspNetCore.Http.FormFileCollection which represents the collection of files sent with the HttpRequest. Files uploaded using the IFormFile technique are buffered in memory or on disk on the server before processing. Inside the action method, the IFormFile contents are accessible as a Stream. In addition to the local file system, files can be saved to a network share or to a file storage service, such as Azure Blob storage.
What can do with this code examples?- Upload a file.
- Upload PDF file.
- You can create a file upload website and many more.
Single file upload in ASP.NET Core Web API
The following code example describes how to upload a file using ASP.NET core web API without using model binding. You can use this codes directly in your project controller.
/// <summary>
/// Upload Sigle file without model binding
/// </summary>
/// <param name="file">file content</param>
/// <param name="fileType">file type</param>
/// <returns></returns>
[HttpPost("UploadFile")]
public async Task<IActionResult> UploadFile(IFormFile file)
{
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await file.CopyToAsync(stream);
}
return Ok(new { file.Length, file.FileName });
}
Single file upload in ASP.NET Core Web API with Model Binding
The previous code examples upload a file using method parameter. The below code example describes how to upload a file with model binding.
/// <summary>
/// Uploads single file with model binding
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost("PostData")]
public async Task<IActionResult> PostData([FromForm] FileRequest request)
{
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await request.FileDetails.CopyToAsync(stream);
}
return Ok(new { request.FileDetails.Length, request.FileDetails.FileName,request.OwnerName });
}
Multiple file upload in ASP.NET Core Web API
/// <summary>
/// Upload Multiple files without model binding
/// </summary>
/// <param name="fileData">file contents</param>
/// <param name="fileType">file type</param>
/// <returns></returns>
[HttpPost("UploadMultiFiles")]
public async Task<IActionResult> UploadMultiFile(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await formFile.CopyToAsync(stream);
}
}
}
// Process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok(new { count = files.Count, size });
}
Multiple file upload in ASP.NET Core Web API with Model Binding
/// <summary>
/// Upload multiple file with model binding
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost("PostMultiFile")]
public async Task<IActionResult> PostMultiFile([FromForm] FileRequest request)
{
//long size = requests.FileDetails..Sum(f => f.Length);
long size= request.Files.Sum(f => f.Length);
foreach (var formFile in request.Files)
{
if (formFile.Length > 0)
{
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await formFile.CopyToAsync(stream);
}
}
}
// Process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok(new { count = request.Files.Count(), size });
}
Model class for Single File Upload
namespace Fileupload.Models
{
public class FileRequest
{
public string OwnerName { get; set; }
public IFormFile FileDetails { get; set; }
}
}
Model Class for Multiple File Upload
namespace Fileupload.Models
{
public class FileRequest
{
public string OwnerName { get; set; }
public IEnumerable<IFormFile> Files { get; set; }
}
}
Thanks