9

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?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
test
  • 2,538
  • 4
  • 35
  • 52
  • 1
    That looks like an IIS limit. – SLaks Nov 14 '11 at 22:54
  • What does that mean exactly please? is it a problem from my code or from the IIS? I don't think I even use IIS, since i just hit run on Visual Studio and a temporary server is started! – test Nov 14 '11 at 23:10
  • 1
    You mean you're using Cassini. I'm not sure. What's the inner exception? – SLaks Nov 14 '11 at 23:13
  • Sry I dont know what Cassini is :/ Inner Exception: Maximum Requested Length Exceeded – test Nov 14 '11 at 23:39

1 Answers1

12

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..

  • thank you for your suggestion. I will try and use it now. after more debugging, I can add the following information: - Response.Headers 'Response.Headers' threw an exception of type 'System.PlatformNotSupportedException' System.Collections.Specialized.NameValueCollection {System.PlatformNotSupportedException} when I open it, the relevant message I found was "This operation requires IIS integrated pipeline mode." – test Nov 15 '11 at 21:09
  • I have tried your suggestion, it does work since the maxRequestlength is very big. I did research this. but still, if a user places something greater than the maxRequestLength, what will happen (although this is very rare to happen), it is still like 'cheating' from the developer's side. isn't there a neater way to do this? PS I do not use async file upload, should I use that instead? – test Nov 15 '11 at 21:55
  • With the approach suggested by @AnandMohanAwasthi is actually a waste of resources. You let the user upload a huge file and then at the server you reject it because it is larger than your lower limit you have in code. I am myself searching for a better solution, but strongly feel the suggested solution is a wrong solution. – Moiz Tankiwala May 29 '15 at 04:49