File upload exception handling RRS feed

  • Question

  • User-2073702470 posted

    Hi, Does anybody know how to force an exception when a file being uploaded to the server is greater than the maxrequestlength value set in the web.config file. Eg if the it's set to 4MB and the user attempts to upload a 10MB file can the exception be trapped and a message gracefully displayed to the user that they have chosen a file that is too large?

    thanks in advance if anyone can point me towards a solution.


    Monday, October 9, 2006 4:36 PM

All replies

  • User-494857339 posted

    The only workaround i've found for this is to set a large maxrequestlength and then trap over-large files in your code. E.g. set maxrequestlength to 20MB but return an error message to the user if they attempt anything over 4MB.

     Another approach I haven't tested might be to use the Application_Error sub in global.asax and do something elegant if that particular error is returned.

    Friday, October 27, 2006 7:04 AM
  • User-2073702470 posted

    Thanks for the reply.  I've previously tried your suggestion of trying to trap the error by testing the maxrequestlength in some C# code but before the code gets a chance to run the app bounces off and displays the page cannot be displayed error message when the file is larger then 20MB. The code works if the file is between 4 and 20 Mb but anything over 20Mb and it fails to run. I think what's needed is a way of checking the file size before it gets uploaded and while it's actually uploading just is case the original small file has been swopped out for a larger one. Any chance of posting a sample of your code just incase I'm missing something? Thanks


    Friday, October 27, 2006 8:47 AM
  • User-494857339 posted
    Yeah, i've had the same problem whereby .NET seems unable to pre-establish the size. All i've done is literally set the maxrequestlength in web.config to a much larger size than I would expect anyone to ever try and be upload to the site, and then in my code said:
    If fupTrack.FileBytes.Length < 10485760 Then
    'process file upload
    '... file is too large, say as much in error text

    (where fupTrack is the fileUploader)

    In terms of error trapping, it does hit the global Application_Error, but I can't seem to override it going to the "page cannot be displayed" error. It might doable by playing around in the IIS  error page configurations...


     Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
                ' Fires when an error occurs
                If Server.GetLastError IsNot Nothing Then
                    If Server.GetLastError.ToString.IndexOf("System.Web.HttpException: Maximum request length exceeded") > -1 Then
                        Response.Write("file too big to deal with!")
                        'with a normal error, the below redirect would work 
                        ' - but not with this one!
                    End If
    Friday, October 27, 2006 10:43 AM