超出fileupload限制时显示错误

时间:2012-06-01 09:49:10

标签: c# asp.net web-config file-upload

在我的页面中,当我上传大小超过4 MB的文件时,它显示连接已重置。原因是文件上载限制小于4 MB。 在link

上有一个解决方案

但是,我希望在同一页面上显示错误而不是重定向。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

您应该在上传(保存文件)

之前阅读文件大小并显示错误(或不显示)
protected void UploadButton_Click(object sender, EventArgs e)
{

    if(FileUpload1.HasFile)
    {
        if(FileUpload1.PostedFile.ContentLength > 4096)
        {
            ErrorLabel.Text = "The file you are trying to upload exceeds the allowed limit.";
        }
        else
        {
            string SavePath = "TheLocationTheFilesSaves";
            FileUpload1.SaveAs(SavePath + FileUpload1.FileName);
            // Do stuff
        }
    }

}
相关问题