简体   繁体   English

使用IValidatableObject在MVC 3中验证上传的图像

[英]Validating uploaded images in mvc 3 with IValidatableObject

I'm running into some trouble validating uploaded images using the new IValidatabeObject interface in asp.net MVC 3. 我在使用asp.net MVC 3中的新IValidatabeObject接口验证上传的图像时遇到了一些麻烦。

I need to do two things: 我需要做两件事:

  • Make sure that the uploaded bytes are really an image 确保上传的字节确实是图像
  • Make sure that this image is 200px wide 确保此图片的宽度为200px

I thought I could accomplish this using a try/catch in the validation block: 我以为可以在验证块中使用try / catch来完成此操作:

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        try
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(this.ImageFile.InputStream);
            if (img.Width != 200)
                yield return new ValidationResult("This picture isn't the right size!!!!");
        }
        catch (Exception ex)
        {
            yield return new ValidationResult("This isn't a real image!") ;
        }
    }

Of course, in a real world production app I would put some lightweight validation in front of this to check the file extension to make sure it is a .jpg to find easy problems without invoking the expensive try/catch mechanism. 当然,在现实世界的生产应用程序中,我会在其前面放置一些轻量级验证来检查文件扩展名,以确保它是.jpg来查找简单问题而无需调用昂贵的try / catch机制。 But ultimately I need to be sure it's a good image, so I do need to make sure that Image.FromStream will parse it successfully. 但是最终我需要确保它是一个好的图像,因此我需要确保Image.FromStream将成功解析它。

The compiler doesn't like this approach and tells me that I can't yield return from a try/catch block. 编译器不喜欢这种方法,并告诉我无法从try / catch块中获得收益。 If I dispense with the yield return and return an IEnumerable directly I get another compiler error: 如果我放弃yield return并直接返回IEnumerable,则会出现另一个编译器错误:

        catch (Exception ex)
        {
            return new ValidationResult[] {new ValidationResult("This isn't a real image!") };
        }

This gives new error: "Cannot return a value from an iterator. Use yield return statement..." 这给出了新的错误:“无法从迭代器返回值。请使用yield return语句...”

I like having the validation logic as part of the model, so I would hate to abandon the IValidatableObject approach, but I don't see a way to make it work. 我喜欢将验证逻辑作为模型的一部分,所以我不想放弃IValidatableObject方法,但是我没有找到一种使之可行的方法。

Move the result to outside the try catch block 将结果移到try catch块之外

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
{
  ValidationResult result = null;
  try
    {
        var img = System.Drawing.Image.FromStream(this.ImageFile.InputStream);
        if (img.Width != 200) 
            result = new ValidationResult("This picture isn't the right size!!!!");
    }
    catch (Exception ex)
    {
        result = new ValidationResult("This isn't a real image!") ;
    }

  if (result != null) 
    yield return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM