我正在尝试解决一个问题。我想用 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;
}
}