How to upload a large file from ASP.NET Core

Programming, error messages and sample code > ASP.NET
To upload files using ASP.NET Core, you can refer to the official documentation: Upload small files with buffered model binding to physical storage.
 
By default, Windows IIS limits the maxRequestLength and maxAllowedContentLength to 4096 KB and 30,000,000 bytes, respectively. To upload a file larger than these limits, you need to overwrite the default settings in your website's root web.config file and modify the ASP.NET Core form settings.
 
Here is an example of how to modify the Program.cs and web.config files to increase the maximum file upload sizes:
 
Program.cs
// using packages.
// ...
using Microsoft.AspNetCore.Http.Features;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// ...

builder.Services.Configure<IISServerOptions>(options=>
{
    // 1024MB
    options.MaxRequestBodySize = 104857600;
});

builder.Services.Configure<FormOptions>(options =>
{
    // 1024MB
    options.MultipartBodyLengthLimit = 104857600;
});

var app = builder.Build();

// ...
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <!-- change the max to 1024 MB -->
        <httpRuntime maxRequestLength="104857600" />
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <!-- change the max to 1024 MB -->
                <requestLimits maxAllowedContentLength="104857600" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>
 
If your application also uses Kestrel settings, you should also overwrite the default settings in the Program.cs file like this:
// using packages.
// ...

var builder = WebApplication.CreateBuilder(args);

builder.Host.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureKestrel((context, options) =>
    {
        options.Limits.MaxRequestBodySize = 104857600;
    });
});

// Add services to the container.
// ...