C
C#15mo ago
carcajou

File upload ASP .net 6

Hey, I made an asp website to try file upload. I use file streaming in order to upload large files. Problem: When uploading a file, I estimated at >30Mo (after a few manual tries) i get an error on the browser saying there was an error (website unreachable) and the file is not uploaded after clicking the button to Post. The website is not crashed and in case of re reaching the address the website can be accessed. I tried to put a breakpoint on my public async Task OnPostAsync() function but it never goes into that function and does the error on the browser. When using a smaller file it does upload without an issue. I tried multiple browser (Firefox, Chrome, Opera) but the error is the same I use a Form and a button. <form method="post" enctype="multipart/form-data"> I'm using Visual Studio I saw that i may have to define a longer length for body requests but it did not worked out options.MultipartBodyLengthLimit
6 Replies
Anchy
Anchy15mo ago
not sure on the ramifications on this but in one of my older projects I would use the DisableRequestSizeLimit attribute because I was using nginx as a reverse proxy which had its own request size limit configuration
Bailey
Bailey15mo ago
Hik, the problem you describe, can be caused by a lot of things. If you use IIS server, this has also limits which can be adjusted by config and also c#. Try to look at those first. Best Regards
carcajou
carcajouOP15mo ago
Hey, When you said IIS server and Nginx you gave me the answer, since i'm on default I use Kestrel and I had configured it for listening addresses. The limit with Kestrel is 28.6Mb which corresponds so Thanks for the answsers Bailey and Anchy. There is a parameter to configure the size of the Request Body Size.
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX15mo ago
In case you did not read the docs: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 Client Code:
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
ServerCode:
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
The field's name in the body when doing the POST, needs to match the binded field in the Controler's Action https://media.discordapp.net/attachments/569261465463160900/1001188081069735936/unknown.png
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
carcajou
carcajouOP15mo ago
My problem has been resolved by the upper answers, your answer doesn't answer my problem and IFormFile is not for large file upload
Want results from more Discord servers?
Add your server