Search code examples
asp.net-mvcasp.net-mvc-2validationasp.net-mvc-2-validation

ASP.NET MVC: Image upload validation


I'm trying to subclass ValidationAttribute in ASP.NET MVC 2 to make something along the lines of an ImageValidator class which would make sure that an uploaded image (from <input type="file">) has the correct mimetype, doesn't exceed the maximum allowed file size, etc.

So where do I start? I get the feeling like images are an exception when it comes to ASP.NET MVC validation attributes and that you always have to manually figure out ways to validate them. For example, what type should the property in my model/view model have?


Solution

  • You don't have access to the file until it is uploaded. You could use a client-side technology like Flash or Silverlight to do all the client side checks. Uploadify is a component that I use for uploading multiple images, it works great.