views:

373

answers:

4

Asp.Net has an upper limit for file uploads. I try to catch this situation on the server side. According to the documentation I found, it should be possible to override Application_Error in Global.asax, but it does not work for me. The second option would be to override OnError of the receiving page, but that also does not work.

Can anybody show some working code on how to catch this error on the server side?

A: 

Rather than catch the error can't you check the size of the file against the maximum size specified in the web.config file? You can use the following to get the max size:

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = section.MaxRequestLength;
Fermin
That code will never be reached because IIS intercepts the upload earlier.
Achim
Depends where you put the above code?
Fermin
A: 

"Can anybody show some working code on how to catch this error on the server side?"

Nope. It's not possible to use code to catch this error, as it occurs before any code is started.

As far as I have found, you can't even specify an alternative error page for this error, the web server just refuses to accept the request when it's too large.

Guffa
If I increase the max upload size in the web.config, it works fine. So the upload must be intercepted by some part of the asp.net runtime. And then it should be possible to somehow catch that point. Do you agree?
Achim
@Achim: If you set the limit so high to practically remove the limit, you can do the check in the code, but then your server is very vulnerable to DoS attacks. Someone could just start a few large uploads, and it would use up all RAM in the server.
Guffa
No, that depends where and when the code is executed. What I say is: If the limit is configured in the asp.ne runtime, then it's also processed in the asp.net runtime. So the request reaches at least asp.net and is not blocked by IIS before that. And then it should be possible to somehow catch it in asp.net. According to documentation that should be possible in the places I mentioned - but it does not!?
Achim
@Achim: Reading a bit about it I see that you can actually catch the error on the server side, but that's pretty useless as the server has cut the connection to stop the upload. The only thing that you can do really is to log the error on the server, the request is already aborted so you can't send anything back to the browser at all.
Guffa
+1  A: 

Uploadify is a jquery and flash uploader that allows you to specify a max size for files to be downloaded. This way you can prevent the user from downloading the file in the first place and dont have to worry about catching it after.

jmein
A: 

Put following in Golobal.asax.cs:

void Application_Error(Object sender, EventArgs e)
        {
            HttpException ex = Server.GetLastError() as HttpException;
            if (ex != null)
            {
                if ((ex.GetHttpCode() == 500 || ex.GetHttpCode() == 400) && ex.ErrorCode == -2147467259)
                {
                    Server.ClearError();
                    Response.Redirect("~/MaximumFileError.aspx", false);
                }
            }
        }

This worked for me, but I'm not sure if it works for all cases.

Sasapet