Showing posts with label File Upload. Show all posts
Showing posts with label File Upload. Show all posts

Friday, October 07, 2022

How to increase file upload size limit in ASP.NET Core

ASP.NET Core 2.0 or 2.1 enforces 30MB (~28.6 MiB) max request body size limit, be it Kestrel and HttpSys. Under normal circumstances, there is no need to increase the size of the HTTP request. But when you are trying to upload large files (> 30MB), there is a need to increase the default allowed limit.

Kestrel is a cross-platform web server for ASP.NET Core and that’s included by default in ASP.NET Core project templates. Kestrel can be used as a standalone server or with a reverse proxy server, such as IIS, Nginx, or Apache

Hosted on IIS

Remember in the ASP.NET, we used to set maxRequestLength in web.config file to increase the default limit of 4MB. Like,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
     <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295"  />
      </requestFiltering>
    </security>  
   </system.webServer>
</configuration>
  

Similarly, for ASP.NET Core application, we can increase the default limit of 30MB by setting maxAllowedContentLength property in the web.config file. The default ASP.NET Core application template doesn’t create the web.config file. It is created when you publish the application. However, you can also add it manually (if not present) to the root of the application with the following code.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <authentication mode="Windows" />
    <httpRuntime enableVersionHeader="false" />
  </system.web>
  <!-- To customize the asp.net core module uncomment and edit the following section. 
  For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <!-- processPath="dotnet" arguments=".\Mobility.Core.Web.API.dll"  update with these values on stage or prod servers-->
    <aspNetCore requestTimeout="00:20:00" processPath="dotnet" arguments=".\myCoreWeb.Core.Web.API.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout">
      <environmentVariables />	 
    </aspNetCore>
	 <security>
        <requestFiltering>
          <!-- This will handle requests up to 50MB -->
          <requestLimits maxAllowedContentLength="52428800" />
        </requestFiltering>
      </security>
  </system.webServer>
</configuration>
<!--ProjectGuid: 82a05c43-b244-4222-a1ff-ebe99b445a14-->

Hope this helps!