如何处理“超出最大长度”错误好不好?

发布于 2024-10-09 06:19:49 字数 462 浏览 0 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶花眉 2024-10-16 06:19:51

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

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

我在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文