If you’re creating a custom page or control that uploads files from a users PC (or somewhere else) you may get this error if the file is too big:
System.Web.HttpException: Maximum request length exceeded.
This is an ASP.NET error before the request is being handled by SharePoint (you can follow the stack trace to understand it a bit more). The challenge developers are facing is handling the exception and producing a nice & customized error message to users. You can easily overcome the problem by modifying the web.config of the Web Application, but that is not good practice and you have to worry about reproducing the modification for scalability and backup/restore purposes.
In a typical custom solution there is a nice solution to this problem. If you use Application Pages (those ASPX files that live in {SharePoint Root}\Templates\Layouts) you can place a web.config in your solution folder within the Layouts folder. Your web.config will go in:
14\TEMPLATE\LAYOUTS\CustomSolutionFolder\web.config
Here’s what you need in the web.config:
<?xml version=“1.0“ encoding=“utf-8“ standalone=“yes“?>
<configuration>
<location path=“CustomUploadPage.aspx“>
<system.web>
<httpRuntime maxRequestLength=“2097151“ />
</system.web>
</location>
</configuration>
web.config’s are very flexible and the location element lets you apply your change exactly on the file that you need. This will work even if you are using an ASCX control located in the _CONTROLTEMPLATES folder. It is also a great approach as it can be packed into WSP’s and be part of an enterprise solution.
The number 2097151 is 2GB (boundary), the maximum SharePoint supported file size. It may be good to reduce this depending on your example.
The next point worth mentioning here is that the above setting alone won’t let you upload files over 50MB unless you configure the Web Application to allow it.
If the Web Application setting is below your file size you will get this exception:
Microsoft.SharePoint.SPException: The specified file is larger than the maximum supported file size.
NOTE: it has an ErrorCode of 2147024872
The good news is that you can handle the above exception with a simple Try…Catch, and that is all you need to facilitate a friendly, customized error message.
Hope this helps!