Link to home
Create AccountLog in
Avatar of clickclickbang
clickclickbang

asked on

Capture Maximum request length exceeded Exception

I am creating a page to accept user files. During testing I received a Maximum request length exceeded error. The fix to this (http://www.powupload.com/System.Web.HttpException-Maximum-request-length-exceeded.aspx) is to up your MaxRequestLength property. However, is there a way to capture this exception before it is thrown? I attempted to wrap the upload file's SaveAs() method in a try/catch but it did not catch the exception.

How do I capture this exception so when a user does exceed the max limit I can alert them accordingly.
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

can you post the code you have so far so we can get a bigger picture. Thanks

Andrew :-)
Avatar of clickclickbang
clickclickbang

ASKER

Not much to it really...

protected void UploadFile_Click(object sender, EventArgs e)
    {
        string UploadDir = Server.MapPath(WebConfigurationManager.AppSettings["ImageHandler_TempDir"]);

        try
        {
            FileUpload1.SaveAs(UploadDir + Path.GetFileName(FileUpload1.FileName));
        }
        catch (System.Web.HttpException ex)
        {
            throw new Exception("I Got You");
        }
        catch (Exception ex)
        {
            throw new Exception("I Got You");
        }
    }

Still throws "Maximum request length exceeded. "

I assume you'd need to catch it else where. I tried :
protected override void OnLoad(EventArgs e)
    {
        try
        {
            base.OnLoad(e);
        }
        catch (System.Web.HttpException ex)
        {
            throw new Exception("I Got You");
        }
        catch (Exception ex)
        {
            throw new Exception("I Got You");
        }
    }

But figured I'd see if any EE Genius knows where or how.

Any ideas on this? If I can't catch this error, how else can I handle a user that attempts to upload a 20MB image?
ASKER CERTIFIED SOLUTION
Avatar of JonathanTheMan
JonathanTheMan

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for your post!