God I have spent nearly 4 days solid trying to work that one out!
Here is the challenge:
Basically there is by a size limit to the file you can upload to a webserver using the good old HtmlInputFile Input. By default this limit is 4Mb and it is set in the machine.config file on the web server:
It is of course possible to override this value in your web.config file and make it web app specific.
The problem occurs when you try to upload something bigger than that... you will get a nasty DNS error because the asp.net worker process got its knickers in a twist on the webserver. It basically reached a memory threshold before it could do anything with the file!
A full explanation can be found here:
Anyway the bottom line is that it displays a nasty error to the user and the people I work for don't like that at all.
As a Google group lover I looked for an answer to that one for a lot longer than I originally thought I would have to and finally found this EXCELLENT article by Jesse Chunn:
Jesse you're the man! The little code snippet you posted is awesome in simplicity and it works great.
The trick is to use the global.asax file and to test the file contentLength for every single request made to the web server.
I post the code again:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
Dim i As Integer
'this number is in bytes, NOT kilobytes!!
Dim iMaxFileSize As Integer = 8000000
For i = 0 To Request.Files.Count - 1
If Request.Files.Item(i).ContentLength > iMaxFileSize Then
Response.Redirect("FileToBig.aspx?filesize=" & Request.Files.Item(i).ContentLength)
A massive thank you to you my friend, you're a star in the developer's world!!!