开发者

How can I intercept an HTTP 404 Response on file Upload when file size exceeds limit?

I've got an MVC3 website that allows users to upload images. If a user tries uploading an image that is greater than the allowed value in my web.config, they are given a 404 response. I'm using a FileUpload control. The user is getting 404开发者_开发问答 response when they submit the form and the file is too large.

Here is an article discussing how to fix the problem, but it only explains how to increase the max size of the file. I don't want to increase the size, but rather give the user a friendly message when they do.

I'd like to intercept this response before it returns, so I can give them a friendly message explaining why they can't upload the file.

Is this possible?


Is an exception being thrown that you can grab during the Application_Error event in the global.asax?

I'm not sure what this particular FileUpload control is that you mention, but with a plain MVC3 application, when I upload a file the Visual Studio web server ("cassini") responds with a 504 error. The framework throws an HttpException when an uploaded file is too large. You could catch it like so:

protected void Application_Error()
        {
            var ex = Server.GetLastError();

            if (ex.Message == "Maximum request length exceeded.")
            {
                //respond another way.
            }
        }

This article is a good reference for the perils of large file uploads: http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx


See this answer, it adds a response check before each response, but it does work: https://stackoverflow.com/a/15532171/1033228

Plus you don't need to increase the size of your max upload size (further than your desired max) thereby opening yourself up to DOS attacks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜