Error "Maximum request length exceeded" in ASP.NET MVC


When I am trying to upload a pdf file(10 MB) using javascript file upload in my ASP.NET MVC application, I am getting this error as below

Maximum request length exceeded

Here is the image of the error

maximum-length-exceeded-mvc-csharp-min.png

How can I resolve it?


Asked by:- Vinnu
1
: 14470 At:- 6/4/2018 8:11:37 AM
C# File upload ASP.NET MVC







2 Answers
profileImage Answered by:- Sam

You are getting this error because maximum length to upload a file is 4MB by default in ASP.NET MVC, so either you upload file's below 4MB(prompt user to upload files below 4 MB) or if you want to remove the size limit constraint, you can set it in your Web.Config file like below

<system.web>
        <httpRuntime maxRequestLength="100000" />
</system.web>

the above code will increase your upload file size limit upto 100MB, for IIS 7 and above you can use the code as below

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

similar this will increase size limit to 100MB in IIS7 & above

Note: maxAllowedContentLength is measured in bytes while maxRequestLength is measured in kilobyte

OR as I have explained above if you want to limit file size to 4MB only but want to handle this exception then you can write this C# code in you Global.asax file

private void Application_Error(object sender, EventArgs e)
{
    var ex = Server.GetLastError();
    var httpException = ex as HttpException ?? ex.InnerException as HttpException;
    if(httpException == null) return;

    if(httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
    {
        //handle the error
        Response.Write("Too big a file, can't upload"); //error
    }
}

Works for ASP.NET 4.0 and above.

From this article https://support.microsoft.com/en-us/help/295626/prb-cannot-upload-large-files-when-you-use-the-htmlinputfile-server-co

Cause:

  • This problem occurs because the default value for the maxRequestLength parameter in the <httpRuntime> section of the Machine.config file is 4096 (4 megabytes). As a result, files that are larger than this value are not uploaded by default.

Resolution

  • In the Machine.config file, change the maxRequestLength attribute of the <httpRuntime>configuration section to a larger value. This change affects the whole computer.
  • In the Web.config file, override the value of maxRequestLength for the application. For example, the following entry in Web.config allows files that are less than or equal to 8 megabytes (MB) to be uploaded:
    <httpRuntime maxRequestLength="8192" />
3
At:- 6/4/2018 11:29:28 AM Updated at:- 9/27/2022 6:40:08 AM
Excellent answer with full details, thank you so much :) 0
By : Vinnu - at :- 6/6/2018 3:34:09 PM
I had to combine this with an existing line for: 0
By : vikas_jk - at :- 9/27/2022 11:12:57 AM


profileImage Answered by:- bhanu

You need to add the below configuration in the main Web.Config file

<system.web>
    <httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>

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

maxRequestLength has 1048576 Kb, and maxAllowedContentLength has 1073741824 bytes.

Above code will work in IIS Express, and Azure also.

2
At:- 11/15/2021 7:16:21 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use