Monday, July 26, 2010

How to handle large file uploads which exceeds the configured request length

Say you have in web config :
<httpRuntime maxRequestLength="4048" />

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"))
            string.Format("~/FileUpload.aspx?Message={0}""Upload file is over the allowed limit"));

Then you can FileUpload.aspx like this:
<html xmlns="">
<head id="Head1" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs 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)
    <form id="form1" runat="server">
        <asp:Label runat="server" ID="lblMessage" />
        <asp:FileUpload runat="server" ID="fuFile" />
        <asp:Button runat="server" ID="btnSave" Text="Save" />

1 comment:

Praveen.S.L said...

Dear friend,

Its a nice post. But i have some issue after handling the Exception.For me Redirection is not happening, the error message i am getting is "Connection Interrupted! The connection to the server was reset while the page was loading."

Please let me know your thoughts on this error.


iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...