Umbraco File Upload Maximum Request Length Exceeded Exception When Uploading Files

One annoyance is the default file restriction size. In most instances when a content editor tries to upload a file, like a 14Md PDF they get a ‘Maximum request length exceeded.’ exception. If this is occurring on your live environment and you have CustomErrors set to ‘RemoteOnly’, it can be troublesome for them to figure out what has gone wrong.

There are two ways of fixing this issue. The first is to set Umbraco to allow bigger files to be uploaded. To do this, open up your web.config file, which can be found in your website’s webroot. In the web.config search for ‘httpRuntime’. In this section, you should see the maxRequestLength. You will need to increase this number to allow for larger uploads. The maxRequestLength by default is set to 4MB, to change the upload to 20MB for example, you would use ‘20192’.

<httpRuntime requestValidationMode="2.0" maxRequestLength="20192" />

You may also want to add the executionTimeout property. This prolongs the period before Umbraco timeouts. Adding executionTimeout would look like this:

<httpRuntime requestValidationMode="2.0" maxRequestLength="20192" executionTimeout="3600" />

If you are using IIS7+ you may bump into this slightly different error:

HTTP Error 404.13 - Not Found
The request filtering module is configured to deny a request that exceeds the request content length.

To fix this will also need to update the requestLimits maxAllowedContentLength property:

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

maxAllowedContentLength is measured in bytes, so 20MB would now be, ‘20971520’. These solutions will probably make the timeout error occur less frequently, but it’s still a shit solution for content editors. One of my strong philosophies when building a website, is that creating a slick and easy back-end that content editors are happy with, is key to how the company perceives the success of a project. If the people who work with the CMS hate it, the whole company will know about it.

ASP.NET Solution

When a content editor uploads a large file that is too large and Umbraco just dies without telling them what the issue is, they’ll get pissed off and complain about it. Instead, it’s worth spending 15 minutes development time to make a solution that stops Umbraco from blowing up and providing feedback to content editors about what went wrong.

To prevent Umbraco blowing up, you will need to hook into the web.configs Application_Error() method. If you haven’t hooked up your Global.ascx file yet to work with custom code, I would recommend you read How To Enable The Global.asax in Umbraco

const int TimedOutExceptionCode = -2147467259;
const string MaxiumRequestLengthExceed = "Maximum request length exceeded";
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
IsMaxRequestException(exception);
}
private void IsMaxRequestException(Exception ex)
{
if (ex == null)
return;
var httpException = ex as HttpException;
if (httpException == null
|| httpException.ErrorCode != TimedOutExceptionCode)
return;
if (ex.InnerException != null 
&& ex.InnerException.Message.Contains(MaxiumRequestLengthExceed))
{
Server.ClearError();
Response.Redirect("~/HTML/FileToLarge.html");
}
}

In our global.config when a ‘File Upload Maximum Request Length Exceeded’ exception occurs, we can catch the exception in Application_Error, using Server.GetLastError(). I’ve put my code in a separate method to make it easier to maintain, but you don’t have to.

In my IsMaxRequestException() method, all I’m really doing is checking the ErrorCode and the text of the inner exception to make sure it is the correct exception. If it is, I clear the exception from the stack and do a redirect to a custom HTML page I created myself. For reference, my FileToLarge.html file, now looks like this:

[<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
The file you are trying to upload is too large.  The maximum size is 20MB
</body>
</html>

.NET 4 Version

If you are using .NET 4, then you can use this much simpler and easier to understand code:

using System.Web.Management;
protected void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError() as HttpException;
if (ex != null
&& ex.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
{
Server.ClearError();
Response.Redirect("~/HTML/FileToLarge.html");
}
}

Conclusion

In today’s tutorial, I’ve covered how to fix upload size issue in Umbraco. You can increase the file size to however large you want too. In my opinion, just making the file size large doesn’t fix the issue. A better solution is to prevent Umbraco blowing up and providing better feedback for content editors. We can do this by hooking into the Application_Error method in the global.ascx.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *