4

In my web.config, I have the file upload size limited to 10 MB:

<httpRuntime maxRequestLength="10000" /> <!-- file size limit in KB -->

On my page, I'm checking to see that the user doesn't upload a file larger than 5 MB:

protected void cvImageSize_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = (fupFile.PostedFile.InputStream.Length <= 5000000);
}

If a user tries to upload a 3 MB file, it uploads fine. If a user tries to upload a 7 MB file, they are shown the error message from cvImageSize.

If a user tries to upload a 13 MB file...the site crashes. I'm not sure exactly what happens, Firefox gives me the page saying "The connection was reset. The connection to the server was reset while the page was loading."

Is there some exception I can catch when the user tries to upload a file with a size greater than the maxRequestLength? I'd like to show the user an error message on the page instead of the site basically crashing.

Steven
  • 18,761
  • 70
  • 194
  • 296
  • 3
    http://stackoverflow.com/questions/3007737/how-can-i-handle-maxrequestlength-exceptions-elegantly http://stackoverflow.com/questions/2756448/how-to-catch-configurationerrorsexception-for-violating-maxrequestlength http://stackoverflow.com/questions/1502371/how-can-you-know-that-current-request-exceeds-maxrequestlength-in-asp-net – Carson63000 Oct 26 '10 at 02:05
  • If any of the answers help you with your problem please mark the relevant as the answer – Shiv Kumar Nov 14 '10 at 17:03

2 Answers2

4

There is also an IIS execution timeout - which may be causing your issue. You can raise that value as well. For more info, check out this article: http://aspnetresources.com/articles/dark_side_of_file_uploads

DarrellNorton
  • 3,901
  • 2
  • 21
  • 21
0

Your basic issue is that the "validation" (cvImageSize_ServerValidate) is really happening on the server. That means the file has to be uploaded first for your server side code to be able to validate.

So when a larger (the web.config setting) file is uploaded the server throws an exception and your code never gets a chance to validate. No matter how you dice it, the user will still have to upload the file to your server first for either the server to complain or for you to validate the size.

Unless you're able to use Html5 (and browsers that are compatible) Html5 File Upload with Progress

Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38