Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET MVC: File upload size limitation

Posted on 2009-07-09
12
Medium Priority
?
1,566 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Accepted Solution

by:
Deathrace earned 1000 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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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