ASP.NET MVC 3:DataAnnotations.FileExtensionsAttribute不起作用

Gre*_*art 8 asp.net-mvc-3

根据MSDN文档,默认情况下FileExtensionsAttribute(.NET 4.5)应该只允许我上传jpg,jpeg,gif和png文件 - 这就是我想要的.

我尝试上传没有属性的jpg,它可以工作.大.然后我将属性添加到我的视图模型中..

[FileExtensions(ErrorMessage = "Please specify a valid image file (.jpg, .jpeg, .gif or .png)")]
public HttpPostedFileBase ImageFile { get; set; }
Run Code Online (Sandbox Code Playgroud)

没有快乐.验证失败,并显示ErrorMessage.最重要的是,似乎没有办法指定任何允许的自定义文件扩展名.我最终扩展了FileExtensionsAttribute并使用我自己的验证逻辑,它按预期工作.但为什么这种方式不起作用?

如果需要,将发布整个控制器并查看.我使用此示例作为上载逻辑的基础,但使用DataAnnotations.FileExtensionsAttribute而不是Microsoft.Web.Mvc.FileExtensions .. 如何在ASP.NET MVC中上载图像?

小智 25

由于System.ComponentModel.DataAnnotations.FileExtensionsAttribute已被密封.我使用MVC 4的包装器.

public class HttpPostedFileExtensionsAttribute : DataTypeAttribute, IClientValidatable
{
    private readonly FileExtensionsAttribute _innerAttribute =
        new FileExtensionsAttribute();

    /// <summary>
    ///     Initializes a new instance of the <see cref="HttpPostedFileExtensionsAttribute" /> class.
    /// </summary>
    public HttpPostedFileExtensionsAttribute()
        : base(DataType.Upload)
    {
        ErrorMessage = _innerAttribute.ErrorMessage;
    }

    /// <summary>
    ///     Gets or sets the file name extensions.
    /// </summary>
    /// <returns>
    ///     The file name extensions, or the default file extensions (".png", ".jpg", ".jpeg", and ".gif") if the property is not set.
    /// </returns>
    public string Extensions
    {
        get { return _innerAttribute.Extensions; }
        set { _innerAttribute.Extensions = value; }
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,
        ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "extension",
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
        };
        rule.ValidationParameters["extension"] = _innerAttribute.Extensions;
        yield return rule;
    }

    /// <summary>
    ///     Applies formatting to an error message, based on the data field where the error occurred.
    /// </summary>
    /// <returns>
    ///     The formatted error message.
    /// </returns>
    /// <param name="name">The name of the field that caused the validation failure.</param>
    public override string FormatErrorMessage(string name)
    {
        return _innerAttribute.FormatErrorMessage(name);
    }

    /// <summary>
    ///     Checks that the specified file name extension or extensions is valid.
    /// </summary>
    /// <returns>
    ///     true if the file name extension is valid; otherwise, false.
    /// </returns>
    /// <param name="value">A comma delimited list of valid file extensions.</param>
    public override bool IsValid(object value)
    {
        var file = value as HttpPostedFileBase;
        if (file != null)
        {
            return _innerAttribute.IsValid(file.FileName);
        }

        return _innerAttribute.IsValid(value);
    }
}
Run Code Online (Sandbox Code Playgroud)


Yur*_*ich 5

使用Extensions属性设置它们.虽然根据文件

文件扩展名,或默认文件扩展名(".png",".jpg",".jpeg"和".gif"),如果未设置该属性.

你可以像你一样设置它ErrorMessage.更可能的问题是它不知道如何评估是否HttpPostedFileBase具有正确的扩展名.您需要使用MVC框架中的那个或创建自己的.