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

  • SEARCH
  • Home
  • 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 8462477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:10:27+00:00 2026-06-10T14:10:27+00:00

I have a file upload feature in my MVC 3 web application and I’m

  • 0

I have a file upload feature in my MVC 3 web application and I’m trying to validate FileSize and FileType using those attributes:

[FileSize(1048576, ErrorMessage = "The image is too big. It should be up to 1MB")]
[FileType(MimeTypes.Image.Jpg, MimeTypes.Image.Jpeg, MimeTypes.Image.Png, "image/pjpeg", "image/x-png", ErrorMessage = "Your image must be a JPG/JPEG or PNG up to 1MB.")]
public HttpPostedFileBase File { get; set; }

HTML is as follows:

<input type="file" size="20" name="File" />
@Html.ValidationMessageFor(x => x.File)

Everything works perfect when the file is selected. But if there is no file selected, I still get FileSize or FileType validation fired and validation errors. How can I avoid this since I don’t want File to be required on POST?

  • 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-10T14:10:28+00:00Added an answer on June 10, 2026 at 2:10 pm

    You will have to modify the FileSize and FileType custom validation attributes so that do not perform any validation if the value is null. For example:

    public class FileSizeAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                // don't validate if value is null
                return null;
            }
    
            // TODO: do whatever validation you were supposed to do
            ...
        }
    }
    

    And it is via the [Required] attribute that you could make the file required.

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

Sidebar

Related Questions

My MVC-3 application has a file upload feature. The file content and content type
I have implemented upload feature in ASP.MVC. I am using jQuery.BlockUI and JQuery.Form plugins
In the web page, I have file upload widget. I need to show this
I have a file upload application. I want to test my Upload controller. For
I have been trying to design a WCF file upload service and am getting
I have a PHP validate statement for a file upload on a HTML form.
I am trying to upload files to a web server using System.Net.WebClient.UploadFile but I
I have a page with html5 drag and drop upload feature and the file
Context: We have some users reporting issues with a file upload feature in our
I'm trying to do a file upload in Flex using it's FileReference class. This

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.