ASP.NET MVC: File upload size limitation

Hi experts

I'm writing an ASP.NET MVC application that lets the user upload files to the server. I need to ensure that the file size is limited to 20MB. I'm not allowed to use Flash, Java Applet or Silverlight and javascript doesn't give me the information about the size of the file. The file size might be checked on the server side, but that's not what I want to do, since I want to limit large uploads already on client side. I can set the maxrequestsize in the web.config, but that results in an error page when uploading files are bigger than 20MB. How can I catch this error and display a message on my upload site? Do I need to pack the file upload in AJAX?

Thanks for your help.
Martin
marbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeathraceCommented:
Hi,
Good question ! Why don't you find the error code for your exception, i think in web.config we can redirect to a differnet page based on error code. Hope you understood how to redirect to errorpage when the user gets exception.

0
marbAuthor Commented:
Thank you for your suggestion. Yes, I know how to redirect based on error code, but there is no error code on the page that comes up. IE says: "The page cannot display"
0
DeathraceCommented:
or else use page level execption handler, use Page.Error += new .......(); in the page where your upload control is resided.
Invoke a method when there is an error and redirect using Response.redirect to error page.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

DeathraceCommented:
or else use page level execption handler, use Page.Error += new .......(); in the page where your upload control is resided.
Invoke a method when there is an error and redirect using Response.redirect to error page.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddayx10Commented:
Deathrace is on the right track here...stealing this from Abel as he and I went through a thread about this awhile back :)

Tells you how to limit the size on client, as well as handle the error elegantly.
http://www.developer.com/db/article.php/10920_3426051_2
0
ddayx10Commented:
...thought about it, admittedly I know little about MVC so that may not work, but from the way the discussion was going it seemed like straight forward ASP.NET discussion. Sorry if its not helpful.
0
marbAuthor Commented:
Thank you for your help. Is this possible in MVC ? I can't see how to do that.
0
ddayx10Commented:
LOL I think we posted at the same time...look up 2 :)
0
marbAuthor Commented:
...back from lunch. Thank you for the additional help. I will check if that solution works with MVC too.
0
marbAuthor Commented:
Hmm, doesn't work in my project. I'm able to catch the exception, but the redirection to an error page does not work:

I have added this code in Global.asax.cs:

protected void Application_Error()
        {
            Exception unhandledException = Server.GetLastError();
            HttpException httpException = unhandledException as HttpException;
            if (httpException == null)
            {
                Exception innerException = unhandledException.InnerException;
                httpException = innerException as HttpException;
            }

            if (httpException != null)
            {
                int httpCode = httpException.GetHttpCode();
               
                switch (httpCode)
                {
                    case (int)HttpStatusCode.Unauthorized:
                        Server.ClearError();
                        Response.Redirect("/Http/Error401");
                        break;
                    case (int)HttpStatusCode.InternalServerError:
                        Server.ClearError();
                        Response.Redirect("/Http/Error500");
                        break;
                    default:
                        break;
                }
            }
        }
0
marbAuthor Commented:
I think I'll go for AJAX. Hope to avoid the error that way.
0
marbAuthor Commented:
Thank you for your help. I wasn't able to work on that issue the last few days. I'll close this question and come back if there should be more to ask.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Frameworks

From novice to tech pro — start learning today.