Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase upload file size in Asp.Net core v3.1

I'm trying to upload multiple files in my .NET Core v3.1 Blazor application, but I can't get passed the 30MB limit.
Searching for this I found Increase upload file size in Asp.Net core and tried the suggestions but it doesn't work.
All found solutions involve changing web.config, but I don't have that file.
Also my application runs in Visual Studio 2019 during development but will also run on Azure as a WebApp.

These are my settings:
program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>().ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = null;
            });
        });

UploadController.cs

[Authorize]
[DisableRequestSizeLimit]
public class UploadController : BaseApiController

ConfigureServices in Startup.cs

services.AddSignalR(e => e.MaximumReceiveMessageSize = 102400000)
    .AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);

services.Configure<FormOptions>(options =>
{
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
    options.MultipartBoundaryLengthLimit = int.MaxValue;
    options.MultipartHeadersCountLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = int.MaxValue;
});

Configure in Startup.cs

app.Use(async (context, next) =>
{
    context.Features.Get<IHttpMaxRequestBodySizeFeature>()
        .MaxRequestBodySize = null;

    await next.Invoke();
});

Did I miss a setting? Can't believe this needs to be so hard.

like image 328
Paul Meems Avatar asked Jan 19 '26 21:01

Paul Meems


1 Answers

Decorate your controller method with the [DisableRequestSizeLimit] attribute.

[HttpPost]
[DisableRequestSizeLimit]
[Route("~/upload")]
public async Task<IActionResult> Upload(...)
{
    return Ok(...);
}
like image 115
hunter Avatar answered Jan 21 '26 12:01

hunter