-3

我正在尝试解决一个问题。我想用 fileupload 控件上传一个最大长度为 10mb 的文件(例如)。我对文件的尺寸进行了所有控制,但是当我尝试上传大于 10mb 的文件时,浏览器会显示“页面错误”页面。

有没有办法通过服务器端拦截错误?

谢谢你们

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        StatusLabel.Text = Server.MapPath("~/prova")
                         + FileUpload1.PostedFile.FileName;

    if (!IsPostBack)
    {
        var config = WebConfigurationManager.OpenWebConfiguration("~");
        var section = config.GetSection("system.web/httpRuntime")
                                     as HttpRuntimeSection;
        section.MaxRequestLength = 10485760*200;
    }

    HyperLink hl = new HyperLink();
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        if (Page.IsValid)
        {
            try
            {
                string filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/prova") + filename);
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be"
                                 + " uploaded. The following error occured: "
                                 + ex.Message;
            }
        }
    }
}

protected void CustomValidator1_ServerValidate(object source,
                                               ServerValidateEventArgs args)
{
    if (FileUpload1.FileContent.Length < 10485760)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}
4

1 回答 1

1

您需要更改 maxRequestLength。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
    <httpRuntime maxRequestLength="102400" executionTimeout="1200" />
</system.web>
</configuration>

如果你想拦截异常,那么你需要在 Global.asax 中处理它。这是 代码示例的链接

于 2013-01-29T10:49:28.600 回答