Solved

ASP.NET MVC: File upload size limitation

Posted on 2009-07-09
12
1,555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP Repeater control - onl;y show if flag is true 7 22
VB.NET Textbox input validation 4 66
Code works but breaks when I add one section 4 30
Calculating percentage 2 31
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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