How to Increase upload file size in ASP.NET Core
By FoxLearn 6/10/2024 8:50:17 AM 102
You'll need to configure the maximum request size in your Startup.cs
file. This can be done in the ConfigureServices
method using the Configure<FormOptions>
method:
public void ConfigureServices(IServiceCollection services) { services.Configure<FormOptions>(options => { options.MultipartBodyLengthLimit = int.MaxValue; // Default value is 128 MB if you don't set // options.MultipartBodyLengthLimit = 52428800; // Limit file size to 50MB }); }
Adjust the value of MultipartBodyLengthLimit
to the desired maximum file size in bytes.
For application running on IIS
services.Configure<IISServerOptions>(options => { options.MaxRequestBodySize = 52428800; // Limit file size to 50MB });
For application running on Kestrel:
services.Configure<KestrelServerOptions>(options => { options.Limits.MaxRequestBodySize = int.MaxValue; // Default value is 30 MB if don't set });
Adding all of the above options will solve problems related to uploading large files.
If you're hosting your ASP.NET Core application on IIS, you may also need to adjust the request limits in the web.config
file:
<configuration> <system.webServer> <security> <requestFiltering> <!-- The maximum allowed content length for uploads, in bytes --> <requestLimits maxAllowedContentLength="52428800" /> <!-- Limit file size to 50MB --> </requestFiltering> </security> </system.webServer> </configuration>
If you want handle request up to 1GB
<system.webServer> <security> <requestFiltering> <!-- Handle requests up to 1 GB --> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>
Ensure that you're not setting the limit too high, as it could pose security risks like denial-of-service attacks.
- How to Minify HTML using WebMarkupMin in ASP.NET Core
- How to fix 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson'
- How to fix System.InvalidOperationException: Scheme already exists: Identity.Application
- The name 'Session' does not exist in the current context
- How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to fix Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing
- How to fix LoginPath not working in ASP.NET Core
- How to fix 'Authorization in ASP.NET Core' with 401 Unauthorized