Last post Oct 27, 2006 10:43 AM by eclbs
Oct 09, 2006 04:36 PM|Brendy|LINK
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.
Oct 27, 2006 07:04 AM|eclbs|LINK
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.
Oct 27, 2006 08:47 AM|Brendy|LINK
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
Oct 27, 2006 10:43 AM|eclbs|LINK
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!