![]() |
|
|
|||||||
![]() |
ASP Net - How to intercept error when httpRuntime maxRequestLength is exceded. |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
I have an application where users need to upload images and in my web.config
file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from being uploaded. I'm also using the HtmlInputFile control to do the uploading. My problem is that when the user's file size exceeds 512k, the page immediately redirects to the "The page cannot be displayed" error page which is very confusing. The use will think that their image is corrupt, or the website has a nasty bug in it. The way this should be handled is instead of showing the nasty "The page cannot be displayed" page, show a friendly page telling the user that they exceeded the file limit and to upload a smaller image. Is there a way to intercept this and do a redirect? and if not, is there any other way to handle this elegantly? -- moondaddy |
|
|
|
|
#2 |
|
Posts: n/a
|
You should be able to handle this in the Application_Error event in the
Global.asax. The code below just shows some example code void Application_Error(object sender, EventArgs e) { SomeStringVar = .Server.GetLastError.Message(); Server.Transfer("Errors.aspx") Server.ClearError() } You could also have a web.config setting that captures that particular Http error number and redirects accordingly. Something like :- <configuration> <system.web> <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> </system.web> </configuration> You could change (or add another entry) to the '500' for whatever error code you want. -- - Paul Glavich Microsoft MVP - ASP.NET "moondaddy" <> wrote in message news:O%... > I have an application where users need to upload images and in my web.config > file I have a setting like this: > > <httpRuntime maxRequestLength="512" /> > > Which restricts image larger than 500k from being uploaded. I'm also using > the HtmlInputFile control to do the uploading. My problem is that when the > user's file size exceeds 512k, the page immediately redirects to the "The > page cannot be displayed" error page which is very confusing. The use will > think that their image is corrupt, or the website has a nasty bug in it. > The way this should be handled is instead of showing the nasty "The page > cannot be displayed" page, show a friendly page telling the user that they > exceeded the file limit and to upload a smaller image. > > Is there a way to intercept this and do a redirect? and if not, is there > any other way to handle this elegantly? > > -- > > > |
|
|
|
#3 |
|
Posts: n/a
|
Thanks Paul. I have a few questions/comments. 1) I pasted the error page
text below so you can see the error coming back. There is no error number so I can't use your example using the web.config file. and 2) I put a break in the global.asax code behind to step through the code and study the error message, but the Application_Error event doesnt fire so this must not be an application error Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) Dim str As String = Server.GetLastError.Message Log(str , EventLogEntryType.Error) End Sub All I know is that the webconfig file's line <httpRuntime maxRequestLength="512" /> tells asp.net what the constraint is. Any other ideas on how to trap this error and handle it? The page cannot be displayed The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties, or you may need to adjust your browser settings. -------------------------------------------------------------------------- Please try the following: a.. Click the Refresh button, or try again later. b.. If you typed the page address in the Address bar, make sure that it is spelled correctly. c.. To check your connection settings, click the Tools menu, and then click Internet Options. On the Connections tab, click Settings. The settings should match those provided by your local area network (LAN) administrator or Internet service provider (ISP). d.. If your Network Administrator has enabled it, Microsoft Windows can examine your network and automatically discover network connection settings. If you would like Windows to try and discover them, click Detect Network Settings e.. Some sites require 128-bit connection security. Click the Help menu and then click About Internet Explorer to determine what strength security you have installed. f.. If you are trying to reach a secure site, make sure your Security settings can support it. Click the Tools menu, and then click Internet Options. On the Advanced tab, scroll to the Security section and check settings for SSL 2.0, SSL 3.0, TLS 1.0, PCT 1.0. g.. Click the Back button to try another link. Cannot find server or DNS Error Internet Explorer -- "Paul Glavich [MVP - ASP.NET]" <-NOSPAM> wrote in message news:... > You should be able to handle this in the Application_Error event in the > Global.asax. The code below just shows some example code > > void Application_Error(object sender, EventArgs e) > { > SomeStringVar = .Server.GetLastError.Message(); > Server.Transfer("Errors.aspx") > Server.ClearError() > } > > You could also have a web.config setting that captures that particular Http > error number and redirects accordingly. Something like :- > <configuration> > <system.web> > <customErrors defaultRedirect="GenericError.htm" > mode="RemoteOnly"> > <error statusCode="500" > redirect="InternalError.htm"/> > </customErrors> > </system.web> > </configuration> > > You could change (or add another entry) to the '500' for whatever error code > you want. > > > > -- > - Paul Glavich > Microsoft MVP - ASP.NET > > > "moondaddy" <> wrote in message > news:O%... > > I have an application where users need to upload images and in my > web.config > > file I have a setting like this: > > > > <httpRuntime maxRequestLength="512" /> > > > > Which restricts image larger than 500k from being uploaded. I'm also > using > > the HtmlInputFile control to do the uploading. My problem is that when > the > > user's file size exceeds 512k, the page immediately redirects to the "The > > page cannot be displayed" error page which is very confusing. The use > will > > think that their image is corrupt, or the website has a nasty bug in it. > > The way this should be handled is instead of showing the nasty "The page > > cannot be displayed" page, show a friendly page telling the user that they > > exceeded the file limit and to upload a smaller image. > > > > Is there a way to intercept this and do a redirect? and if not, is there > > any other way to handle this elegantly? > > > > -- > > > > > > > > |
|
|
|
#4 |
|
Posts: n/a
|
Hello,
I think Paul's suggestion should work. I put following code in Application_Error, and it worked: If Request.TotalBytes > MaxValue Then Server.ClearError() Response.Clear() Response.Write "The file is too large" End If |
|
|
|
#5 |
|
Posts: n/a
|
Hmmm, I tried your code below and I get the same result as before, I get the
"The page cannot be displayed" page and it seems as though the Application_Error event isn't firing. The page is actually a user control sitting on a aspx page and the aspx page inherits from a base page. I don't know if this would cause the event to not fire. I put some breaks in the global.asax and I saw the code execute on the Application_BeginRequest event, but it clearly isn't hitting the Application_Error event. I also put a watch on the Response object in the Application_BeginRequest event and saw the there was no value for TotalBytes. below is what was listed in the watch window for TotalBytes" TotalBytes <error: an exception of type: {System.Web.HttpException} occurred> Integer Any other ideas? -- "[MSFT]" <> wrote in message news:gjS9SH$... > Hello, > > I think Paul's suggestion should work. I put following code in > Application_Error, and it worked: > > If Request.TotalBytes > MaxValue Then > > Server.ClearError() > > Response.Clear() > > Response.Write "The file is too large" > > End If > |
|
|
|
#6 |
|
Posts: n/a
|
Hmmm, I tried your code below and I get the same result as before, I get the
"The page cannot be displayed" page and it seems as though the Application_Error event isn't firing. The page is actually a user control sitting on a aspx page and the aspx page inherits from a base page. I don't know if this would cause the event to not fire. I put some breaks in the global.asax and I saw the code execute on the Application_BeginRequest event, but it clearly isn't hitting the Application_Error event. I also put a watch on the Response object in the Application_BeginRequest event and saw the there was no value for TotalBytes. below is what was listed in the watch window for TotalBytes" TotalBytes <error: an exception of type: {System.Web.HttpException} occurred> Integer Any other ideas? -- "[MSFT]" <> wrote in message news:gjS9SH$... > Hello, > > I think Paul's suggestion should work. I put following code in > Application_Error, and it worked: > > If Request.TotalBytes > MaxValue Then > > Server.ClearError() > > Response.Clear() > > Response.Write "The file is too large" > > End If > |
|
|
|
#7 |
|
Posts: n/a
|
Hello,
Here is the web form code I use to test, you may test them on your server to see if they can work: <form id="Form1" method="post" enctype="multipart/form-data" action="WebForm1.aspx" runat="server"> <INPUT id="oFile" type="file" runat="server" NAME="oFile"> <br> <asp:button id="btnUpload" type="submit" text="Upload" runat="server"></asp:button> <asp Runat="server"> <asp:Label id="lblUploadResult" Runat="server"></asp:Label> </asp </form> Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click Dim strFileName As String Dim strFilePath As String Dim strFolder As String strFolder = "C:\Test\" 'Get the name of the file that is posted. strFileName = oFile.PostedFile.FileName strFileName = Path.GetFileName(strFileName) 'Create the directory if it does not exist. If (Not Directory.Exists(strFolder)) Then Directory.CreateDirectory(strFolder) End If 'Save the uploaded file to the server. strFilePath = strFolder & strFileName If File.Exists(strFilePath) Then lblUploadResult.Text = strFileName & " already exists on the server!" Else oFile.PostedFile.SaveAs(strFilePath) lblUploadResult.Text = strFileName & " has been successfully uploaded." End If 'Display the result of the upload. frmConfirmation.Visible = True End Sub The problem may be related the web control you use. Therefore, we may begin with a basic web form for test. Luke |
|
|
|
#8 |
|
Posts: n/a
|
hey, this is my suggestion. so simple. i think u have to handle this with javascript. in file upload control, u can check for the file size before send t the server. if it is large, give an error. if it is in limit, send t the server. if u need to get the size from the server, u have to add a hidden fiel and set the max value from the web config in the page_load event. tell if it worked. regards, Charit - Charit ----------------------------------------------------------------------- Posted via http://www.codecomments.co ----------------------------------------------------------------------- |
|
|
|
#9 |
|
Posts: n/a
|
If my posted file is bigger than maxRequest in httpRuntime i can trap i in Application_Error but the redirect to an error page doesn't works an the process die... I obtain an outpage like classic Cannot find server.... I didn't find a way to redirect user to an error page... You suggest to use js client-side to check file size... but j client-side cannot do it ... can you explain your idea in a block j code?? Charith wrote: > *hey, this is my suggestion. > > so simple. > i think u have to handle this with javascript. > > in file upload control, u can check for the file size before send t > the server. if it is large, give an error. if it is in limit, send t > the server. > > if u need to get the size from the server, u have to add a hidde > field and set the max value from the web config in the page_loa > event. > > tell if it worked. > > regards, > Charith - atar ----------------------------------------------------------------------- Posted via http://www.codecomments.co ----------------------------------------------------------------------- |
|
|
|
#10 |
|
Posts: n/a
|
just check on this, i didnt try. [url]http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20573809.html[/url - Charit ----------------------------------------------------------------------- Posted via http://www.codecomments.co ----------------------------------------------------------------------- |
|