Friday, July 24, 2009

Handle file upload timeout

Put the following in the Global.asax.vb, it will check the MaxRequestLength and send you to the error page before uploading the file.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

' Fires at the beginning of each request, session not available here

'check for file upload size,

'MaxRequestLength: Int32, the maximum request size in kilobytes. The default size is 4096 KB (4 MB).

'ContentLength: Int64, the number of bytes of data to send to the Internet resource.

If ConfigurationManager.GetSection("system.web/httpRuntime").MaxRequestLength IsNot Nothing Then

Dim maxSize As Integer = CInt(ConfigurationManager.GetSection("system.web/httpRuntime").MaxRequestLength)

maxSize = maxSize * 1024

If Request.ContentLength > maxSize Then

'do not redirect to itself, it will create an infinite loop

Response.Redirect("~/ErrorPage.aspx?ErrMsg=You have reached the maximum upload file limit, please try again!")

End If

End If

End Sub


Reference:
Dealing With the ASP.Net Upload File Size Problem