views:

2677

answers:

5

Application able to record error in OnError, but we are not able to do any redirect or so to show something meaningfull to user. Any ideas? I know that we can set maxRequestLength in web.config, but anyway user can exceed this limit and some normal error need to be displayed.

A: 

You could check the length of the postedfile (FileUpload.PostedFile.ContentLength), to see if it's below the limit or not and simply showing a friendly error message if necessary.

snomag
Not works - asp.net cannot get to this point.
Sergey Osypchuk
Alright, that's new for me, I thought you can check the content length first than decide to upload the file / reject it.
snomag
Thanks for the info, I just checked it out and it does fail. Good to know as I've used it before and didn't realize there's an issue with it.
snomag
A: 

You should be able to catch the error in Global.asax - OnError() handler. But unfortunately, your initial request will be finished and you will not be able to render the same upload page with some error notification to the user.

What you can do at most is to display a friendly error page with simple redirect clause from within OnError() handler and on that page to have some back link or similar functionality to return the user to the page where he triggered the error in the first place.

Update:

I had to implement exact check upon file upload recently and what i came up with is SWFUpload library which totally met my requirements and also has a lot of additional features. I used it along with jquery wrapper provided by Steve Sanderson. More details can be found here.

The point is, that flash is able to detect the file size on client side and react properly if this case is met. And i think this is exactly what you need.

Further more, you can implement flash detection check if you want to gracefully degerade to native upload button0 in case client does not have flash installed.

ljubomir
That's true, I am able to catch exception in OnError, however, Response.Redirect() do nothing.
Sergey Osypchuk
+1  A: 

Unfortunately, you probably will require IIS7 and catch this with a custom handler, since IIS6 will never get to the stage where it can see the size of the file. It can only know the size when it's done uploading or has got an error.

This is a known problem in ASP.NET. Another (lame) alternative is to handle this earlier in the request and maybe use a flash-based uploader. John links to several in below link.

Update: Jon Galloway seemed to have looked deeper into this problem and it seems like a RIA-uploader is the only sensible alternative, since IIS seems to always have to swallow the file AND THEN tell you that it's to large.

Seb Nilsson
Our server runs IIS7, can u provide some link/keywords where to look for an answer how to overcome this issue?
Sergey Osypchuk
+4  A: 

As you say you can setup maxRequestLength in your web.config (overriding the default 4MB of your machine.config) and if this limits is exceeded you usually get a HTTP 401.1 error.

In order to handle a generic HTTP error at application level you can setup a CustomError section in your web.config within the system.web section:

<system.web>
   <customErrors mode=On defaultRedirect=yourCustomErrorPage.aspx />
</system.web>

Everytime the error is showed the user will be redirected to your custom error page.

If you want a specialized page for each error you can do something like:

<system.web>    
   <customErrors mode="On" defaultRedirect="yourCustomErrorPage.aspx">
     <error statusCode="404" redirect="PageNotFound.aspx" />
   </customErrors>
</system.web>

And so on.

Alternatively you could edit the CustomErrors tab of your virtual directory properties from IIS to point to your error handling pages of choice.

The above doesn't seem to work for 401.x errors - this code-project article explains a workaround for a what seems to be a very similar problem: Redirecting to custom 401 page

JohnIdol
Thanks, this is complete guide about handling errors, I end up exactly with same piece of xml.However, this is not helping case when request length exceeded.
Sergey Osypchuk
See last edit and let me know if you come up with something
JohnIdol
A: 

Sergei,

Per JohnIdol's answer, you need to set up a custom error page for the 413 statuscode. e.g.:

 <customErrors mode="On" defaultRedirect="~/Errors/Error.aspx">
     <error statusCode="413" redirect="~/Errors/UploadError.aspx"/>
 </customErrors>

I know because I had to solve the same problem on a client project, and this was the solution that worked for me. Unfortunately it was the only solution I found... it wasn't possible to catch this particular problem in code; for instance, checking the length of the posted file as snomag has suggested, or catching an error in global.asax. Like you, I also had tried these other approaches before I came up with a working solution. (actually I eventually found this somewhere on the web when I was working on my problem).

Hope that helps.

Mike Strother