【问题标题】:Fileupload large file size error handlingFileupload 大文件大小错误处理
【发布时间】:2016-06-27 09:13:58
【问题描述】:

我的 web.config 设置如下:

<httpRuntime maxRequestLength="5000" executionTimeout="120"/>

我在 Global.asax 中的错误处理代码:

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    //Exception ex;

    string sourcepath = System.IO.Path.GetFileName(Request.Path);
    if (string.Compare(System.IO.Path.GetFileName(Request.Path), "vendorMassUpload.aspx", StringComparison.OrdinalIgnoreCase) == 0)
    {
        System.Exception lastException = Server.GetLastError();
        HttpException httpException = (HttpException)lastException;
        int httpCode = httpException.GetHttpCode();
        int errorCode = httpException.ErrorCode;
        if (errorCode == -2147467259)
        {
            Server.ClearError();
            Response.Redirect("~/vendorManagement/vendorMassUpload.aspx?fileTooLarge=true");
        }
    }
}

我在测试中用于上传的文件大小为 4999 KB。 它将到达Response.Redirect,但它只会在 Internet Explorer 中显示This page can’t be displayed

如果我在ButtonUpload_Click 中的throw new Exception("testing exception");if (errorCode == -2147467259) 发表评论,一切正常。

出了什么问题?

我要做的就是重定向用户并在他们上传大文件时向他们显示自定义消息,而不是告诉他们This page can’t be displayed

【问题讨论】:

  • 默认情况下,如果输入文件大小大于maxrequestlength,ASP .NET 会终止处理请求而不抛出异常。此解决方案可能会有所帮助:stackoverflow.com/questions/2756448/…
  • @Tetsuya Yamamoto 感谢您的链接,我发现我的代码在 Visual Studio 开发 Web 服务器中不起作用,但它在本地 IIS 中起作用。但是为什么?

标签: c# asp.net


【解决方案1】:

一般当上传文件大小超过web.config文件中的maxRequestLength限制时会返回HttpException,因此您可以尝试根据异常消息进行重定向处理:

void Application_Error(object sender, EventArgs e)
{
    string sourcepath = System.IO.Path.GetFileName(Request.Path);
    if (string.Compare(System.IO.Path.GetFileName(Request.Path), "vendorMassUpload.aspx", StringComparison.OrdinalIgnoreCase) == 0)
    {
        System.Exception lastException = Server.GetLastError();
        HttpException httpException = (HttpException)lastException;
        int httpCode = httpException.GetHttpCode();
        int errorCode = httpException.ErrorCode;

        // check if returned exception contains "exceed"
        if (lastException != null && lastException is HttpException && lastException.Message.Contains("exceed"))
        {
            Server.ClearError();
            // redirect to custom error page
            Response.Redirect("~/vendorManagement/vendorMassUpload.aspx?fileTooLarge=true");
        }
    }
}

希望这可能会有所帮助,CMIIW。

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多