views:

5024

answers:

5

I'm writing an upload function, and have problems catching "System.Web.HttpException: Maximum request length exceeded" with files larger than the specified max size in httpRuntimein web.config (max size set to 5120). I'm using a simpled <input> for the file.

The problem is that the exception is thrown before the upload button's click-event, and the exception happens before my code is run. So how do I catch and handle the exception?

EDIT: The exception is thrown instantly, so I'm pretty sure it's not a timeout issue due to slow connections.

+1  A: 

You can solve this by increasing the maximum request length in your web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <httpRuntime maxRequestLength="102400" />
    </system.web>
</configuration>

The example above is for a 100Mb limit.

GateKiller
Yes, and no. You push the limit further, but it doesn't really handle the exception. You'll still get the same problem if someone tries to upload 101+ Mb. The limit really needs to be 5 Mb.
Marcus L
+6  A: 

As GateKiller said you need to change the maxRequestLength. You may also need to change the executionTimeout in case the upload speed is too slow. Note that you don't want either of these settings to be too big otherwise you'll be open to DOS attacks.

The default for the executionTimeout is 360 seconds or 6 minutes.

You can change the maxRequestLength and executionTimeout like so:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <httpRuntime maxRequestLength="102400" executionTimeout="1200" />
    </system.web>
</configuration>

EDIT:

If you want to handle the exception regardless then as has been stated already you'll need to handle it in Global.asax. Here's a link to a code example

Jonathan Parker
Thanks for your reply, but as I said in my comment to GK's answer this doesn't really solve my problem. It's not a timeout issue either, as the exception is thrown instantly. I'll edit the question to make that more clear.
Marcus L
See my edit for handling the error in Global.asax
Jonathan Parker
Yup, that did the trick. Thanks!
Marcus L
The code example url points to a page that is not available...can anyone fix this?
deostroll
+6  A: 

There is no easy way to catch such exception unfortunately. What I do is either override the OnError method at the page level or the Application_Error in global.asax and check if it was a max request failure then transfer to an error page.

protected override void OnError(EventArgs e) .....


private void Application_Error(object sender, EventArgs e)
{
 if (GlobalHelper.IsMaxRequestExceededEexception(this.Server.GetLastError()))
 {
  this.Server.ClearError();
  this.Server.Transfer("~/error/UploadTooLarge.aspx");
 }
}

It's a hack but the code below works for me

const int TimedOutExceptionCode = -2147467259;
public static bool IsMaxRequestExceededEexception(Exception e)
{
 // unhandeled errors = caught at global.ascx level
 // http exception = caught at page level

 Exception main;
 var unhandeled = e as HttpUnhandledException;

 if (unhandeled != null && unhandeled.ErrorCode == TimedOutExceptionCode)
 {
  main = unhandeled.InnerException;
 }
 else
 {
  main = e;
 }


 var http = main as HttpException;

 if (http != null && http.ErrorCode == TimedOutExceptionCode)
 {
  // hack: no real method of identifing if the error is max request exceeded as 
  // it is treated as a timeout exception
  if (http.StackTrace.Contains("GetEntireRawContent"))
  {
   // MAX REQUEST HAS BEEN EXCEEDED
   return true;
  }
 }


 return false;
}
Damien McGivern
Thanks. OnError didn't work, but Application_Error did. We actually have a handler for this, but someone had turned it off in the code.
Marcus L
A: 

Hi solution mentioned by Damien McGivern, Works on IIS6 only,

It does not work on IIS7 and ASP.NET Development Server. I get page displaying "404 - File or directory not found."

Any ideas?

EDIT:

Got it... This solution still doesn't work on ASP.NET Development Server, but I got the reason why it was not working on IIS7 in my case.

The reason is IIS7 has a built-in request scanning which imposes an upload file cap which defaults to 30000000 bytes (which is slightly less that 30MB).

And I was trying to upload file of size 100 MB to test the solution mentioned by Damien McGivern (with maxRequestLength="10240" i.e. 10MB in web.config). Now, If I upload the file of size > 10MB and < 30 MB then the page is redirected to the specified error page. But if the file size is > 30MB then it show the ugly built-in error page displaying "404 - File or directory not found."

So, to avoid this, you have to increase the max. allowed request content length for your website in IIS7. That can be done using following command,

appcmd set config "SiteName" -section:requestFiltering -requestLimits.maxAllowedContentLength:209715200 -commitpath:apphost

I have set the max. content length to 200MB.

After doing this setting, the page is succssfully redirected to my error page when I try to upload file of 100MB

Refer, http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx for more details.

Vinod T. Patil
Sorry! I have added my query as an Answer, I don't know how to add comments to existing posts.
Vinod T. Patil
You just need more rep. to comment posts. See the faq for more details about what you can/can't do with your current rep.
kbok