Thursday 3 January 2013

You get "Maximum request length exceeded" when you upload big files

I came across this issue in an MVC 3 project I'm working on at the moment, where when the user tried to upload files greater than 4mb in size, they were getting this exception.
If you're using IIS 7, a lot of answers online seem to point to increasing the maxAllowedContentLength attribute for the requestLimits element in the web.config.
Others also say if you're using IIS 6, you need to increase the maxRequestLength attribute for the httpRuntime element, also in the web.config.
I had to do both.
Within <system.web>, add:

And within <system.webServer>, add:
IMPORTANT:

Both of these values must match. In this case, my max upload is 1024 megabytes.

maxRequestLength has 1048576 KILOBYTES, and maxAllowedContentLength has 1073741824 BYTES.

It's quite easy to overlook this difference in requirements for each attribute, and mismatching these values can cause other errors.
I also answered this question on StackOverflow.