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
How can I resolve it?
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:
Resolution
<httpRuntime maxRequestLength="8192" />
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly