PDA

Click to See Complete Forum and Search --> : Handling the System.Web.HttpException: Maximum request length exceeded error


cesark
Dec 23rd, 2004, 10:47 AM
I limited the upload file size in my web.config to 320KB, thus:

<configuration>
<system.web>

<httpRuntime maxRequestLength="320" />

</system.web>
</configuration>


After that, when I try to upload a file bigger than 320KB from the FormUpload.aspx page, from the localhost machine, I see an error page for less than 1 second, and after that I am redirected to the original FormUpload.aspx page. If I try the same thing from a client machine, I receive the ugly error page:

Address bar: http://locahost/WebProject/UploadForm.aspx
The page cannot be displayed
The page you are looking for is currently unavailable. The web site…


To solve that problem I wrote the FormUpload.aspx page thus:

<%@ Page Language="VB" Debug="True" %>

<script language="VB" runat="server">


Sub Page_Load(Sender as object, e as EventArgs)

If Session("ImageTooLarge") = True Then

lblTooLarge.Text() = "The file is bigger than 320KB!!"
Session("ImageTooLarge") = False

Else

If IsPostBack Then
lblTooLarge.Text() = "The file is Ok! Smaller or equal than 320KB"
End If

End If

End Sub




Sub Page_Error(Sender as object, e as EventArgs)

Dim appException As System.Exception = Server.GetLastError()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appException, HttpException)
' Verify the expected error
If checkException.GetHttpCode = 400 And checkException.ErrorCode = -2147467259 Then

Session("ImageTooLarge") = True
Server.ClearError()
Response.Redirect("FormUpload.aspx")
End If
End If

End Sub

</script>


<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form_1" enctype="multipart/form-data" method="post" runat="server">

<input type="file" id="attached_file" runat="server" /><br><br>
<asp:button ID="buton_1" runat="server" Text="Test the file" /><br><br><br>
<asp:label ID="lblTooLarge" ForeColor="#FF0000" runat="server"></asp:label>

</form>
</body>
</html>


And if the file uploaded from this page is bigger than 320KB a beautiful message text label appears saying “The file is bigger than 320KB!!”, and I said cool!. But this only happens from the localhost machine, when I test the same page in a client machine the same ugly error page appears (The page cannot be displayed…).

Somebody knows why this happens?

Thank you,
Cesar

plenderj
Dec 26th, 2004, 02:13 PM
Let me answer your question with a question. Have you tried putting code into your global.asax file?

cesark
Dec 27th, 2004, 07:12 AM
Hi plenderj,

Yes, now I am trying it with the global.asax file, and it works fine on the server, but on the client machine appears the error (The page cannot be found...). The strange thing is that only happens with this specific error, for the rest of the errors or other code I put in the global.asax file, the client machine works accordingly (fine).

In the 5 point of this page: http://bcp.bowstreet.com/library/exchange/detail/PageControls/file_upload.htm

I read:

"Note: If you use Microsoft Internet Explorer to upload a file that is more than the maximum file size specified the Web browser interprets that as a Web page that cannot be found. As a result, you will be shown the default error message for your application server when the browser cannot find a Web page.

On the page that contains the File Upload control, you may want to include some text explaining the what the user can expect if they try to upload a file whose size is greater than the maximum file size property. There is no graceful way to handle the event (for example using an error handler method) in which a user tries to upload a file greater than the size allowed because the processing takes place within the browser, not in the Automation Engine."


Do you understand it?
I think that if I can handle that error on the server (simulating a client), Why isn' t it possible on a 'real' client machine?

Thank you

TomGibbons
Dec 27th, 2004, 08:49 AM
Well basically that page explains that if the client is running Internet Explorer and they try to upload something that's too big, it's going to give them the 'Page Cannot Be Found' error regardless. So you're best off Putting a warning on the page saying warning them that this is what they can expect.

:)

cesark
Dec 27th, 2004, 09:49 AM
I also understand this, anyway I find very strange that I can handle the error when I test the same page on server side with an IE browser as well, simulating a client, and the same page on a client machine it responds in a different way.