Snippet
<httpRuntime maxRequestLength="4048" />
Cause:
When you try to upload a file grater then 4MB you will get an error with the
status of 404.13. To catch this error we have to utilise Application_Error as
.net framework does not generate a request object and initiate a page lifecycle
for this type of request. So no events get fired, but first of all you will end
up in a error page.
What we can do is:
In Global.asax file:
protected void Application_Error(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().Contains("FileUpload.aspx") &&
HttpContext.Current.Error.InnerException.Message.Contains("Maximum request length exceeded"))
{
HttpContext.Current.ClearError();
HttpContext.Current.Response.Redirect(
string.Format("~/FileUpload.aspx?Message={0}", "Upload file is over the allowed limit"));
}
}
Then you can FileUpload.aspx like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string message = this.Request.QueryString.Get("Message");
if (!string.IsNullOrEmpty(message)) this.lblMessage.Text = message;
else this.lblMessage.Visible = false;
}
protected void Save(object sender, EventArgs e)
{
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="lblMessage" />
<asp:FileUpload runat="server" ID="fuFile" />
<asp:Button runat="server" ID="btnSave" Text="Save" />
</div>
</form>
</body>
</html>