asp.netiismaxrequestlength

ERR_CONNECTION_RESET: The connection was reset when uploading a large file


I had a mysterious error where a file greater than 4MB generated a random error. Later on I realized it was caused due to the http maxrequestlength . An image cannot be greater than 4MB when uploaded by default.

I know that this can change from the web.config file.

When I tried to cater for this error, by displaying another page, a different error started popping up. When debugging, the program enters application_error immediately.

When executing Server.GetLastError() Exception generated:

[System.Web.HttpUnhandledException] {"Exception of type 'System.Web.HttpUnhandledException' was thrown."} System.Web.HttpUnhandledException

the stack trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.businessprofile_aspx.ProcessRequest(HttpContext context) in c:\Users\Mattew\AppData\Local\Temp\Temporary ASP.NET Files\root\4ea30077\8f66786f\App_Web_h5fmhavk.4.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

If I try any code inside the application_error method, e.g. redirecting, still the error page: Error 101 (net::ERR_CONNECTION_RESET): The connection was reset. is displayed.

Questions

  1. How should this error be handled? Can it be handled before hand? So this error is not displayed? ( I tried using jquery to get the file size before and check it but I'm finding it too complex

  2. If Question 1 is not 'answerable', is there a way to intercept this error and display a friendly error?


Solution

  • Try this out.

    Under system web in web.config

    add this line..

      <system.web>
    <httpRuntime executionTimeout="999" maxRequestLength="2097151"/>
    

    Then you need to check the file size

    if (AsyncFileUpload1.HasFile)
            {
                string FileName = Path.GetFileName(AsyncFileUpload1.PostedFile.FileName);
                string Extension = Path.GetExtension(AsyncFileUpload1.PostedFile.FileName);
                string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
                string FilePath = Server.MapPath("~/xl/" + FileName);
                double filesize = (double)AsyncFileUpload1.FileBytes.Length;
                if (filesize < 106496)
                {
                   //do something
                }
                else
                {
                    Response.Write("File size must be less than 2MB.");
                }
    

    If you find it useful, please mark it as your answer else let me know..