1

I encounter "maximum length exceeded" error when I try to upload a document which is 9MB in size. I know that the issue will be solved if httpRuntime maxRequestLength and requestLengthDiskThreshold in web.config are increased but what I am looking for is how I can nicely handle the error and show the message to the user. I did try to use Application_Error event in global ascx but the event is not fired. The reason might be from Server.Transfer from DNN PageBase class's OnError method.
Specifications:

  • NET 3.5 SP1 (ASP.NET)
  • IIS 6
  • DotNetNuke 5.4.4(2)

It is quite urgent and your suggestion is much appreciated. Thanks

bdukes
  • 152,002
  • 23
  • 148
  • 175
Thurein
  • 2,536
  • 7
  • 34
  • 49

1 Answers1

2

I had a similar issue a few months back, this post was extremely helpful: http://www.velocityreviews.com/forums/showpost.php?p=3794467&postcount=8

Basically you add code to the global.asax codebehind to sniff every page request. If a file is attached, it checks the file size before the upload occurs to your actual page.. works like a champ.

I needed it in VB, so just incase you do as well.. I'll save you the conversion ;)

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

    Dim runtime As System.Web.Configuration.HttpRuntimeSection = System.Web.Configuration.WebConfigurationManager.GetSection("system.web/httpRuntime")
    Dim maxRequestLength As Integer = (runtime.MaxRequestLength - 100) * 1024

    Dim context As HttpContext = CType(sender, HttpApplication).Context

    If context.Request.ContentLength > maxRequestLength Then
        Dim pro As IServiceProvider = CType(context, IServiceProvider)
        Dim workerRequest As HttpWorkerRequest = DirectCast(pro.GetService(GetType(HttpWorkerRequest)), HttpWorkerRequest)

        If workerRequest.HasEntityBody Then
            Dim requestLength As Integer = workerRequest.GetTotalEntityBodyLength

            Dim initialBytes As Integer = 0

            If workerRequest.GetPreloadedEntityBody IsNot Nothing Then initialBytes = workerRequest.GetPreloadedEntityBody.Length

            If Not workerRequest.IsEntireEntityBodyIsPreloaded Then
                Dim buffer As Byte() = New Byte(511999) {}
                Dim receivedBytes As Integer = initialBytes

                While (requestLength - receivedBytes) >= initialBytes
                    initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length)
                    receivedBytes += initialBytes

                End While
                initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes)
            End If

            Response.Redirect("~/errorPages/MaxLength.htm")

        End If
    End If



End Sub
ewitkows
  • 3,528
  • 3
  • 40
  • 62
  • Thanks ... ewitkows ... I'm going to check it out when I get back to office on Monday ... – Thurein Dec 23 '10 at 15:34
  • May I ask you why do you need that entire code inside the 'If workerRequest.HasEntityBody Then' conditional? Why can't you simply check if 'If context.Request.ContentLength > maxRequestLength' then call the redirect? – Wagner Danda da Silva Filho Jun 28 '11 at 20:18
  • The code needs to consume the whole file because otherwise the browser will detect that the upload did not complete and assume that the connection was broken and display a "page not found" error. – Robert Tanenbaum Nov 09 '11 at 20:14
  • @wdanda I was wondering the same thing. However, by keeping in the last `initialBytes = workerRequest.ReadEntityBody(buffer, requestLength-receivedBytes)` causes a 50% chance of getting `Value does not fall within the expected range.` – Matt R May 07 '13 at 19:08