CodexBloom - Programming Q&A Platform

Slow file uploads in ASP.NET Core when using IFormFile with large files

πŸ‘€ Views: 61 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-25
aspnet-core file-upload performance C#

Hey everyone, I'm running into an issue that's driving me crazy. I'm experiencing important performance optimization when uploading large files (around 100MB) in my ASP.NET Core 6.0 application using the `IFormFile` interface. The upload process seems to take an unusually long time, and I've noticed that it often leads to timeouts on the server side. I've already configured the maximum request body size in my `Startup.cs` file like this: ```csharp public void ConfigureServices(IServiceCollection services) { services.Configure<FormOptions>(options => { options.MultipartBodyLengthLimit = 104857600; // 100 MB }); } ``` Despite this, the uploads can take over 30 seconds, and during that time, CPU usage spikes significantly. I also implemented a cancellation token to handle potential timeout exceptions, but it doesn’t seem to help. Here's the upload action method I'm using: ```csharp [HttpPost] public async Task<IActionResult> UploadFile(IFormFile file, CancellationToken cancellationToken) { if (file.Length > 0) { var path = Path.Combine(Directory.GetCurrentDirectory(), "uploads", file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream, cancellationToken); } return Ok(); } return BadRequest(); } ``` I've tried testing this on different servers and even in a local development environment, but the scenario continues. I've also looked into increasing the server's timeout settings, but I’m concerned that this might only mask the underlying question. Are there any best practices or recommendations for improving the performance of file uploads in ASP.NET Core, especially for large files? Would implementing a streaming approach make a important difference? Any insights would be greatly appreciated! How would you solve this? Hoping someone can shed some light on this.