如何处理“最大长度超过"?错误很好吗? [英] How to handle the "Maximum length exceeded" error nicely?

查看:127
本文介绍了如何处理“最大长度超过"?错误很好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

几个月前,我遇到了类似的问题,这篇帖子非常有帮助:

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

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

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.

我在VB中需要它,所以以防万一您也这样做..我将为您保存转换;)

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

这篇关于如何处理“最大长度超过"?错误很好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆