ASP.NET - how to show a error page when uploading big file (Maximum request length exceeded)?

给你一囗甜甜゛ 提交于 2019-11-30 16:48:21

问题


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.


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


The best way to handle large uploads is to use a solution that implements an HttpModule that will break the file up into chunks. Any of the pre-rolled solutions out there should allow you to limit file size. Plenty of others have posted links to those on this page so I won't bother. However, if you don't want to bother with that you can handle this in your app's Global.asax Application_Error event. If your application is .NET 4.0, stick this block of code in there:

    if (ex.InnerException != null && ex.InnerException.GetType() == typeof(HttpException) && ((HttpException)ex.InnerException).WebEventCode == System.Web.Management.WebEventCodes.RuntimeErrorPostTooLarge)
    {
        //Handle and redirect here, you can use Server.ClearError() and Response.Redirect("FileTooBig.aspx") or whatever you choose
    }

Source: http://justinyue.wordpress.com/2010/10/29/handle-the-maximum-request-length-exceeded-error-for-asyncfileupload-control/

If you're running on an earlier framework, try the code here (it's in VB, but it's easy to translate): http://www.webdeveloper.com/forum/showthread.php?t=52132




回答6:


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.



来源:https://stackoverflow.com/questions/126167/asp-net-how-to-show-a-error-page-when-uploading-big-file-maximum-request-leng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!