Solved

ASP.NET MVC: File upload size limitation

Posted on 2009-07-09
12
1,542 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now