Hi @mc
I am not hosting iis but hosting dotnet run.
You are using Kestrel, so you need to configure request body limit on Kestrel, code like this:
builder.WebHost.ConfigureKestrel(opt =>
{
opt.Limits.MaxRequestBodySize=null; //disable the request body limit.
});
Then, configure the FormOptions as below:
builder.Services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
});
In the Razor page Post method, get the upload file from the IFormCollection
, instead of using IFormFile
and model binding.
public class FileUploadModel : PageModel
{
private IWebHostEnvironment _environment;
public FileUploadModel(IWebHostEnvironment environment)
{
_environment = environment;
}
[RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
public async Task OnPostAsync([FromForm] IFormCollection formData)
{
var file = formData.Files["file"];
if (file != null && file.Length > 0)
{
var filePath = Path.Combine(_environment.WebRootPath, "uploads", file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
}
}
The Razor page as below: without using model binding, just using an input file element.
@page
@model RazorWebApp.Pages.FileUploadModel
<form method="post" enctype="multipart/form-data">
<input id="file" type="file" name="file" />
<input type="submit" />
</form>
The result as below: the upload file size 197MB.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion