如何正确处理“超出最大长度”错误?

时间:2010-12-23 10:20:38

标签: asp.net dotnetnuke dotnetnuke-5

当我尝试上传大小为9MB的文档时,遇到“超出最大长度”错误。我知道如果web.config中的httpRuntime maxRequestLengthrequestLengthDiskThreshold增加,问题就会解决,但我正在寻找的是如何很好地处理错误并向我们展示消息用户。我确实尝试在全局ascx中使用Application_Error事件,但事件未被触发。原因可能来自Server.Transfer来自DNN PageBase类的OnError方法     产品规格:

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

非常紧急,非常感谢您的建议。 感谢

1 个答案:

答案 0 :(得分:2)

几个月前我有类似的问题,这篇文章非常有帮助:http://www.velocityreviews.com/forums/showpost.php?p=3794467&postcount=8

基本上,您将代码添加到global.asax codebehind以嗅探每个页面请求。如果附加了一个文件,它会在上传到您的实际页面之前检查文件大小..就像一个冠军。

我需要在VB中使用它,所以只要你也这样做..我会为你节省转换;)

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