我正在努力解决问题。我想上传一个文件上传控件的文件,最大长度为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;
}
}
答案 0 :(得分:1)
您需要更改maxRequestLength。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="1200" />
</system.web>
</configuration>
如果你想拦截异常,那么你需要在Global.asax中处理它。这是一个链接 code example