maxRequestLength and FileUpload

J

Jay

I have been trying to use maxRequestLength to stop users from uploading
large files. It put this code in the web.config file:


The upload works fine.
When I select a large file I get the "Page cannot be displayed" message,
which is correct.

The problem is that I am trying redirect the user to a meaningful error
page or back to the upload page showing an error message. To do this I put
the code below in Page_Error or Application_Error. I have epxerimented with
both.

Server.ClearError()
Session("UPLOADERROR") = "yes"

Response.Redirect("ErrorPage.aspx")

No matter what I do I get the "Page cannot be displayed" messaage, rather
than being redirected. When this error message is displayed the URL in the
browser is showing update.aspx, not the error page.apsx. If I refresh I
get the Upload.aspx page. When I try to upload a large file and I set a
break point on Page_Error I can step to Reponse.redirect("ErrorPage.aspx")
but it does not redirect to ErrorPage.aspx.

I have also experimented with the web.config setting customErrors but I get
the same thing.

Help!!

Jay
 
S

Steve C. Orr [MVP, MCSD]

Seva Voloshin sent me this code, which displays a friendly error message when somebody upload a file that is too large:

private void Page_Load(object sender, System.EventArgs e)

{

if(!IsPostBack)

{

//use session to retrieve value

object imageStatus = Session["ImageTooLarge"];

if(imageStatus != null && (bool)imageStatus == true)

{

Label1.Visible = true;

Session["ImageTooLarge"] = null;

}

}

}







protected void Application_Error(Object sender, EventArgs e)

{

if(System.IO.Path.GetFileName(Request.Path) == "WebForm1.aspx")

{

HttpException checkException = Server.GetLastError() as HttpException;



//Verify the expected error

if(checkException.GetHttpCode() == 400 && checkException.ErrorCode == -2147467259)

{

//Error 400 = bad request, user tried to upload a file that's too large

Session["ImageTooLarge"] = true;

Server.ClearError();

Response.Redirect("WebForm1.aspx");

}

}

}
 
G

Guest

I'm trying this in VS Beta2 and ASP.NET 2.0, and no matter what I try I
cannot forward to an Error Page or back to the original page. I keep getting
the "The Page Cannot be Displayed" message.

Thru many hours of testing I have discovered several facts:
1) The transfer from Global.ascx actually Redirects to original page, but
after the load event the "... Cannot Display..." message occurs.
2) After the error, and redirection to original page; the ContentLength
property has the same value as before the error. It's as if the page is
still trying to upload the file. Is there anyway to cancel the page handler,
or Upload process?
3) I can capture the error on the Page's Error event, but the same problems
occur.
4) After "...Cannot Display..." message it is very difficult to get back to
download page. Refresh and the Back Button don't work 95% of the time.
Usually I have to close Browser and open new Browser.

Thanks


Steve C. Orr said:
Seva Voloshin sent me this code, which displays a friendly error message when somebody upload a file that is too large:

private void Page_Load(object sender, System.EventArgs e)

{

if(!IsPostBack)

{

//use session to retrieve value

object imageStatus = Session["ImageTooLarge"];

if(imageStatus != null && (bool)imageStatus == true)

{

Label1.Visible = true;

Session["ImageTooLarge"] = null;

}

}

}







protected void Application_Error(Object sender, EventArgs e)

{

if(System.IO.Path.GetFileName(Request.Path) == "WebForm1.aspx")

{

HttpException checkException = Server.GetLastError() as HttpException;



//Verify the expected error

if(checkException.GetHttpCode() == 400 && checkException.ErrorCode == -2147467259)

{

//Error 400 = bad request, user tried to upload a file that's too large

Session["ImageTooLarge"] = true;

Server.ClearError();

Response.Redirect("WebForm1.aspx");

}

}

}








Jay said:
I have been trying to use maxRequestLength to stop users from uploading
large files. It put this code in the web.config file:


The upload works fine.
When I select a large file I get the "Page cannot be displayed" message,
which is correct.

The problem is that I am trying redirect the user to a meaningful error
page or back to the upload page showing an error message. To do this I put
the code below in Page_Error or Application_Error. I have epxerimented with
both.

Server.ClearError()
Session("UPLOADERROR") = "yes"

Response.Redirect("ErrorPage.aspx")

No matter what I do I get the "Page cannot be displayed" messaage, rather
than being redirected. When this error message is displayed the URL in the
browser is showing update.aspx, not the error page.apsx. If I refresh I
get the Upload.aspx page. When I try to upload a large file and I set a
break point on Page_Error I can step to Reponse.redirect("ErrorPage.aspx")
but it does not redirect to ErrorPage.aspx.

I have also experimented with the web.config setting customErrors but I get
the same thing.

Help!!

Jay
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
Martindap

Latest Threads

Top