Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5398
  • Last Modified:

System.Web.HttpException: Maximum request length exceeded

When I try to upload files over 4 megabytes I receive the below error:
[HttpException (0x80004005): Maximum request length exceeded.]
   System.Web.HttpRequest.GetEntireRawContent() +895
   System.Web.HttpRequest.GetMultipartContent() +57
   System.Web.HttpRequest.FillInFormCollection() +257
   System.Web.HttpRequest.get_Form() +50
   System.Web.UI.Page.GetCollectionBasedOnMethod() +70
   System.Web.UI.Page.DeterminePostBackMode() +128
   System.Web.UI.Page.ProcessRequestMain() +63

I know that I can set maxRequestLength parameter web.config (or machine.config) to allow larger file uploads and that a default one is 4mgs. What I actually would like to do is limit it to 300kb. I have added this code in my application:
try
                        {
if((UploadFile.PostedFile.ContentLength > FileLength) || (UploadFile.PostedFile.InputStream.Length > FileLength))
                                    {
                                          litUploadFile.Text = "<font color='red'>File Size must be less than 300kb</font>";
                                          litUploadFile.Visible = true;
                                    }
                                    else
{//Upload functionality}
catch
                        {
                              litUploadFile.Text = "<font color='red'>File Size must be less than 300kb</font>";
                              litUploadFile.Visible = true;
                        }

Also I tried to encounter for a page level error and added this code:
private void Page_Load(object sender, System.EventArgs e)
            {
Page.Error += new EventHandler(Page_Error);
}

            private void Page_Error(object sender, EventArgs e)
            {
                  litUploadFile.Text = "<font color='red'>File Size must be less than 300kb</font>";
                  litUploadFile.Visible = true;
            }

I still receive the error I mentioned above. What am I doing wrong? How can catch/trap this error? I don't want to do it inside the global.asax.
                                    {


0
TransBind
Asked:
TransBind
  • 3
1 Solution
 
daniel_ballaCommented:
Hi TransBind,
the problem is that the exception is not thrown from within your try block, it is thrown by the processrequest which happens long before the page load event where your try block probably is.
However, your page error method could sort this, but you need to clear the error, otherwise it bubbles up.
so you should have something like:
...
Exception ex = Server.GetLastError();
Server.ClearError();
if (ex is System.Web.HttpException)
    Server.Transfer("SomeErrorPageWithDUmmyText.aspx");

Cheers!
0
 
daniel_ballaCommented:
or if you don't want to redirect to another page but want to output on the same page you can just use respose write

such as:

private void Page_Error(object sender, System.EventArgs e)
        {
            Exception ex = Server.GetLastError();
            Server.ClearError();
            if (ex is System.Web.HttpException)
                Response.Write("<font color='red'>File Size must be less than 300kb</font>");
            Response.Write("there was some other error: " + ex.ToString());
        }


Also, don't forget to register the handler for event: in InitializeComponent as:
this.Error += new System.EventHandler(this.Page_Error);

hope this helps
Dani
0
 
daniel_ballaCommented:
daniel_balla,
> Also, don't forget to register the handler for event: in InitializeComponent as:
> this.Error += new System.EventHandler(this.Page_Error);

Or, you don't have to do this if the autoevenwireup in your page is true. but if it is false, you will have to.
0
 
TransBindAuthor Commented:
Hmm ... unfortunately your suggested solution doesn't work.

However, I found a way to accomplish it. I missunderstood the way upload functionality works in .net. It seems that you have to upload the file before you can check its file size. So by increasing the size of the allowed reqest like so
<system.web>
  <httpRuntime maxRequestLength="81192" ........ />
  </system.web>

I could now check for very large files and whether or not they exceed 300kb allowed size.

As far as your suggestion everything seems correct but it still throws the same error. Perhaps I need to handle it inside of global.asax file ... not sure. Suggestions? For learning puproses how did u know that this error is thrown by processrequest? You could tell this by Exception details or this is just your general knowledge?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now