CodexBloom - Programming Q&A Platform

Difficulty Handling File Uploads with IFormFile in ASP.NET Core 6 - implementing Large Files

๐Ÿ‘€ Views: 72 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-06
asp.net-core iformfile file-upload C#

I recently switched to I'm building an ASP.NET Core 6 application where users can upload files through a form. I'm using `IFormFile` to handle the uploads, but I'm running into issues when uploading large files (over 10 MB). I'm receiving a `400 Bad Request` behavior when the file exceeds a certain size, and it doesn't seem to be hitting my controller action at all. I have the following code in my controller: ```csharp [HttpPost] public async Task<IActionResult> UploadFile(IFormFile file) { if (file == null || file.Length == 0) { return BadRequest("No file uploaded."); } var filePath = Path.Combine("C:\Uploads", file.FileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } return Ok("File uploaded successfully."); } ``` In my `Startup.cs`, I have set the maximum request body size like this: ```csharp services.Configure<FormOptions>(options => { options.MultipartBodyLengthLimit = 20 * 1024 * 1024; // 20 MB }); ``` However, when I try uploading a 15 MB file, I get the following behavior in the response: ``` Invalid request content. The request payload is too large. ``` I checked the `web.config` file as well but Iโ€™m running on Kestrel, so that shouldnโ€™t be an scenario, right? I've also confirmed that `Kestrel` has its own `Limits` settings, but I donโ€™t know how to configure that properly to allow larger uploads. I would appreciate any insights on how to resolve this scenario or if there are additional configurations I need to check for large file uploads in ASP.NET Core 6. This is part of a larger CLI tool I'm building. Any pointers in the right direction?