Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:31:11+00:00 2026-06-07T05:31:11+00:00

Basically I have a page with a file input that allows end-users to upload

  • 0

Basically I have a page with a file input that allows end-users to upload some document.

I have a controller which looks like this

public class MyController : Controller 
{
  [HttpPost]
  public ActionResult MyAction(MyViewModel model, HttpPostedFileBase document)
  { .. }
}

What I want to achieve is: when a user attempts to upload a file that is larger than the maxContentLength setting allows a validation error should be added to the ModelState and the user should be returned to the page where the form he submitted was. Handling the error in an ExceptionFilter and redirecting to a custom page isn’t a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:31:13+00:00Added an answer on June 7, 2026 at 5:31 am

    You can’t send requests that are larger than the maxContentLength setting. The web server will kill this request much before it had even chance to reach your application and give you the possibility to handle this error. So if you want to handle it you will have to increase the value of maxContentLength to a reasonably large number and then inside your controller action check for the ContentLength of the uploaded file.

    [HttpPost]
    public ActionResult MyAction(MyViewModel model, HttpPostedFileBase document)
    { 
        if (document != null && document.ContentLength > MAX_ALLOWED_SIZE)
        {
            ModelState.AddModelError("document", "your file size exceeds the maximum allowed file size")
            return View(model);
        }
    
        ...
    }
    

    But obviously a much cleaner solution is to directly handle this at your view model. You don’t need a HttpPostedFileBase argument. That’s what view models are intended for:

    public class MyViewModel
    { 
        [MaxFileSize(MAX_ALLOWED_SIZE)]
        public HttpPostedFileBase Document { get; set; }
    
        ... some other properties and stuff
    }
    

    where MaxFileSize is obviously a custom attribute that you could easily implement.

    And now your POST action becomes more standard:

    [HttpPost]
    public ActionResult MyAction(MyViewModel model)
    { 
        if (!ModelState.IsValid)
        {
            return View(model);
        }
    
        ...
    }
    

    You may take a look at the following example I wrote.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which basically allows an admin user to create manager user
Basically I have a single page on my site that I want any php
Basically I have a single page site that scrolls when the user clicks on
I have a page that will basically be used to concatenate a bunch of
Basically, I have this webpage that is the header on any page you go
I have a query that is more complicated, but basically creates an HTML page
I have an MVC app that is basically one page with a bunch of
Ok, so basically I have a jspf template file with a backing FragmentBean that
So basically I have a page that has it's own CSS. On my server
Basically I have an IFRAME that displays a given page. I am using jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.