How to resolve "Maximum request length exceeded"

Programming, error messages and sample code

Large file uploads in ASP.NET

Uploading files via the FileUpload control gets tricky with big files. The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."


Server Error in '/' Application.
------------------------------
Maximum request length exceeded.


Solution:

The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, add below code to web.config:

<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>

IIS7(and later version) has a built-in request scanning which imposes an upload file cap which defaults to 30MB. To increase it, you also need to add the lines below:

<system.webServer>
   <security>
      <requestFiltering>
         <requestLimits maxAllowedContentLength="3000000000" />
      </requestFiltering>
   </security>
</system.webServer>

Note: maxAllowedContentLength is measured in bytes while maxRequestLength is measured in kilobytes.

Warning: If you are using our shared hosting plan, we do not recommend you to upload large files via script which will slow down your site loading speed. You can upload it via FTP instead.