File Upload - httpRuntime maxRequestLength

Discussion in 'ASP .Net' started by Matt Chubb, Dec 6, 2005.

  1. Matt Chubb

    Matt Chubb Guest

    I've searched the forums and the net and have not found a solution to this
    upload problem, any suggestions/help in this matter would be greatly
    appreciated.

    I'm trying to trap and display an appropriate error message when a file
    upload is more than the maxRequestLength of the httpRuntime tag within
    web.config/machine.config and prevent the "Page Cannot be Displayed" error
    message appearing and the ASP.NET worker process bombing out (Doesn't look
    particularly pretty to the user).

    I've tried creating an UploadHandler, which inherits IHttpModule and runs a
    custom BeginRequest handler containing the following code snippet, which in
    theory should work:

    try
    {
    HttpApplication app = (HttpApplication)sender;
    HttpContext cxt = app.Context;
    HttpWorkerRequest hwr =
    (HttpWorkerRequest)cxt.GetType().GetProperty("WorkerRequest",
    (BindingFlags)36).GetValue(cxt, null);

    if(cxt.Request.ContentType.IndexOf("multipart/form-data") > -1)
    {
    int intContentLength =
    Convert.ToInt32(hwr.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
    if(intContentLength > 3072000)
    {
    app.Context.Response.Redirect("FileSizeException.aspx");
    }
    }
    }
    catch(Exception ex)
    {

    }

    This code functions as it should, but does not perform the redirect and
    displays the "Page Cannot be Displayed" error message, due to the same
    reason as discussed earlier.

    I apologise for the intense post, but I would really like to find a solution
    to this problem or any comments anyone has regarding this issue.

    Thanks in advance

    Matt
    Matt Chubb, Dec 6, 2005
    #1
    1. Advertising

  2. Matt Chubb

    Bruce Barker Guest

    the problem is the http 1.1 protocol. there is no server message to stop the
    upload (browser request). so when the max length is hit, asp.net closes the
    request stream to stop the upload. this of course causes the browser to
    display the error.

    -- bruce (sqlwork.com)



    "Matt Chubb" <> wrote in message
    news:e$Y%23SNn%...
    > I've searched the forums and the net and have not found a solution to this
    > upload problem, any suggestions/help in this matter would be greatly
    > appreciated.
    >
    > I'm trying to trap and display an appropriate error message when a file
    > upload is more than the maxRequestLength of the httpRuntime tag within
    > web.config/machine.config and prevent the "Page Cannot be Displayed" error
    > message appearing and the ASP.NET worker process bombing out (Doesn't look
    > particularly pretty to the user).
    >
    > I've tried creating an UploadHandler, which inherits IHttpModule and runs
    > a custom BeginRequest handler containing the following code snippet, which
    > in theory should work:
    >
    > try
    > {
    > HttpApplication app = (HttpApplication)sender;
    > HttpContext cxt = app.Context;
    > HttpWorkerRequest hwr =
    > (HttpWorkerRequest)cxt.GetType().GetProperty("WorkerRequest",
    > (BindingFlags)36).GetValue(cxt, null);
    >
    > if(cxt.Request.ContentType.IndexOf("multipart/form-data") > -1)
    > {
    > int intContentLength =
    > Convert.ToInt32(hwr.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
    > if(intContentLength > 3072000)
    > {
    > app.Context.Response.Redirect("FileSizeException.aspx");
    > }
    > }
    > }
    > catch(Exception ex)
    > {
    >
    > }
    >
    > This code functions as it should, but does not perform the redirect and
    > displays the "Page Cannot be Displayed" error message, due to the same
    > reason as discussed earlier.
    >
    > I apologise for the intense post, but I would really like to find a
    > solution to this problem or any comments anyone has regarding this issue.
    >
    > Thanks in advance
    >
    > Matt
    >
    >
    Bruce Barker, Dec 6, 2005
    #2
    1. Advertising

  3. Matt Chubb

    Joerg Jooss Guest

    Bruce Barker wrote:

    > the problem is the http 1.1 protocol. there is no server message to
    > stop the upload (browser request). so when the max length is hit,
    > asp.net closes the request stream to stop the upload. this of course
    > causes the browser to display the error.


    Quite the contrary, HTTP 1.1 is the solution -- at least in theory. If
    the client would send an Expect: Continue header, the server would be
    able to reject an oversized upload with a 417 reponse.

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Dec 6, 2005
    #3
  4. Matt Chubb

    Matt Chubb Guest

    Is there no way IIS can handle this and redirect to a custom error page
    configured by us?

    Thanks

    Matt



    "Joerg Jooss" <> wrote in message
    news:...
    > Bruce Barker wrote:
    >
    >> the problem is the http 1.1 protocol. there is no server message to
    >> stop the upload (browser request). so when the max length is hit,
    >> asp.net closes the request stream to stop the upload. this of course
    >> causes the browser to display the error.

    >
    > Quite the contrary, HTTP 1.1 is the solution -- at least in theory. If
    > the client would send an Expect: Continue header, the server would be
    > able to reject an oversized upload with a 417 reponse.
    >
    > Cheers,
    > --
    > http://www.joergjooss.de
    > mailto:
    Matt Chubb, Dec 7, 2005
    #4
  5. Matt Chubb

    Joerg Jooss Guest

    Matt Chubb wrote:

    > Is there no way IIS can handle this and redirect to a custom error
    > page configured by us?


    Probably by implementing something at the ISAPI level, but I'm no
    expert here :-/

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Dec 7, 2005
    #5
  6. Matt Chubb

    Mirek Endys Guest

    Is there a soultion for this???
    Will it work on WinServer 2003 ???
    Is there an patch???

    Thanks

    Mirek

    "Bruce Barker" <> wrote in message
    news:OBDE$qo%...
    > the problem is the http 1.1 protocol. there is no server message to stop
    > the upload (browser request). so when the max length is hit, asp.net
    > closes the request stream to stop the upload. this of course causes the
    > browser to display the error.
    >
    > -- bruce (sqlwork.com)
    >
    >
    >
    > "Matt Chubb" <> wrote in message
    > news:e$Y%23SNn%...
    >> I've searched the forums and the net and have not found a solution to
    >> this upload problem, any suggestions/help in this matter would be greatly
    >> appreciated.
    >>
    >> I'm trying to trap and display an appropriate error message when a file
    >> upload is more than the maxRequestLength of the httpRuntime tag within
    >> web.config/machine.config and prevent the "Page Cannot be Displayed"
    >> error message appearing and the ASP.NET worker process bombing out
    >> (Doesn't look particularly pretty to the user).
    >>
    >> I've tried creating an UploadHandler, which inherits IHttpModule and runs
    >> a custom BeginRequest handler containing the following code snippet,
    >> which in theory should work:
    >>
    >> try
    >> {
    >> HttpApplication app = (HttpApplication)sender;
    >> HttpContext cxt = app.Context;
    >> HttpWorkerRequest hwr =
    >> (HttpWorkerRequest)cxt.GetType().GetProperty("WorkerRequest",
    >> (BindingFlags)36).GetValue(cxt, null);
    >>
    >> if(cxt.Request.ContentType.IndexOf("multipart/form-data") > -1)
    >> {
    >> int intContentLength =
    >> Convert.ToInt32(hwr.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));
    >> if(intContentLength > 3072000)
    >> {
    >> app.Context.Response.Redirect("FileSizeException.aspx");
    >> }
    >> }
    >> }
    >> catch(Exception ex)
    >> {
    >>
    >> }
    >>
    >> This code functions as it should, but does not perform the redirect and
    >> displays the "Page Cannot be Displayed" error message, due to the same
    >> reason as discussed earlier.
    >>
    >> I apologise for the intense post, but I would really like to find a
    >> solution to this problem or any comments anyone has regarding this issue.
    >>
    >> Thanks in advance
    >>
    >> Matt
    >>
    >>

    >
    >
    Mirek Endys, Dec 21, 2005
    #6
  7. Matt Chubb

    ojemuyiwa

    Joined:
    Aug 17, 2006
    Messages:
    2
    Any fix on this guys? searching high and low for a solution and 5 hours in with a lot of anticipation. Please help with a url or text for this solution!
    ojemuyiwa, Aug 17, 2006
    #7
  8. Matt Chubb

    xmanta

    Joined:
    Oct 30, 2006
    Messages:
    1
    xmanta, Oct 30, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Row
    Replies:
    1
    Views:
    1,421
    Peter Row
    Dec 4, 2003
  2. Goncalo
    Replies:
    2
    Views:
    1,092
    Goncalo
    Jan 7, 2004
  3. moondaddy
    Replies:
    25
    Views:
    18,582
    Hermit Dave
    Aug 30, 2004
  4. =?Utf-8?B?QW5keSBTdGVwaGVucw==?=

    When does the httpRuntime maxRequestLength setting kick in?

    =?Utf-8?B?QW5keSBTdGVwaGVucw==?=, May 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    796
    bruce barker \(sqlwork.com\)
    May 8, 2006
  5. David Elliott
    Replies:
    0
    Views:
    484
    David Elliott
    Jan 27, 2005
Loading...

Share This Page