I am getting the error Maximum request length exceeded when I am trying to upload a video in my site.
How do I fix this?
And just in case someone's looking for a way to handle this exception and show a meaningful explanation to the user (something like "You're uploading a file that is too big"):
//Global.asax
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 (((System.Web.HttpException)httpException.InnerException).WebEventCode == System.Web.Management.WebEventCodes.RuntimeErrorPostTooLarge)
{
//handle the error
Response.Write("Too big a file, dude"); //for example
}
}
(ASP.NET 4 or later required)
If you can't update configuration files but control the code that handles file uploads use HttpContext.Current.Request.GetBufferlessInputStream(true)
.
The true
value for disableMaxRequestLength
parameter tells the framework to ignore configured request limits.
For detailed description visit https://msdn.microsoft.com/en-us/library/hh195568(v=vs.110).aspx
If you are using IIS for hosting your application, then the default upload file size is 4MB. To increase it, please use this below section in your web.config
-
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
</configuration>
For IIS7 and above, you also need to add the lines below:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
Note:
maxRequestLength
is measured in kilobytesmaxAllowedContentLength
is measured in bytes which is why the values differ in this config example. (Both are equivalent to 1 GB.)
I had to edit the C:\Windows\System32\inetsrv\config\applicationHost.config
file and add <requestLimits maxAllowedContentLength="1073741824" />
to the end of the...
<configuration>
<system.webServer>
<security>
<requestFiltering>
section.
As per This Microsoft Support Article
The maximum request size is, by default, 4MB (4096 KB)
This is explained here.
The above article also explains how to fix this issue :)
I was tripped up by the fact that our web.config file has multiple system.web sections: it worked when I added < httpRuntime maxRequestLength="1048576" /> to the system.web section that at the configuration level.