Link to home
Start Free TrialLog in
Avatar of TransBind
TransBind

asked on

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


Avatar of daniel_balla
daniel_balla

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!
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
ASKER CERTIFIED SOLUTION
Avatar of daniel_balla
daniel_balla

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TransBind

ASKER

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?