Solved

ASP.NET MVC: File upload size limitation

Posted on 2009-07-09
12
1,547 Views
Last Modified: 2013-11-08
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
0
Comment
Question by:marb
  • 6
  • 3
  • 3
12 Comments
 
LVL 9

Expert Comment

by:Deathrace
ID: 24811686
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
 

Author Comment

by:marb
ID: 24811727
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24811761
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 9

Accepted Solution

by:
Deathrace earned 250 total points
ID: 24811845
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
 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 250 total points
ID: 24811986
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
 
LVL 20

Expert Comment

by:ddayx10
ID: 24811998
...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
 

Author Comment

by:marb
ID: 24812003
Thank you for your help. Is this possible in MVC ? I can't see how to do that.
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24812007
LOL I think we posted at the same time...look up 2 :)
0
 

Author Comment

by:marb
ID: 24812530
...back from lunch. Thank you for the additional help. I will check if that solution works with MVC too.
0
 

Author Comment

by:marb
ID: 24812776
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
 

Author Comment

by:marb
ID: 24814378
I think I'll go for AJAX. Hope to avoid the error that way.
0
 

Author Comment

by:marb
ID: 24856746
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question