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开发者_JAVA技巧 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?
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.
There isn't any way that I'm aware of to do any of this type of validation until the full file is uploaded. So I think the simplest approach would be to go ahead and save it to disk and then do your validation on the resulting file at the end of the process... 99% of the time the files will pass validation anyway...
Note: It would be possible to do some of this validation in javascript on the client, but it's probably not worth the trouble (IMO).
精彩评论