DataAnnotation Regular Expression始终为文件输入返回false

ONY*_*NYX 2 asp.net-mvc asp.net-mvc-4

我已经为RegularExpression数据注释尝试了很多正则表达式,以检查文件扩展名是否是图像,并且它总是返回false,例如我也尝试了FileExtension属性,但它在jquery.validation上创建了一个错误.我正在使用ASP.NET MVC 4 Razor

[RegularExpression(@"^.*\.(jpg|gif|jpeg|png|bmp)$", 
ErrorMessage = "Please use an image with an extension of .jpg, .png, .gif, .bmp")]
public string MyImage { get; set; }
Run Code Online (Sandbox Code Playgroud)

这是我的标记

    <div class="editor-field">            
        @Html.TextBoxFor(x => x.DepartmentImage, new { type = "file" })            
        @Html.ValidationMessage("DepartmentImageError")
        @Html.ValidationMessageFor(model => model.DepartmentImage)
    </div>
Run Code Online (Sandbox Code Playgroud)

有人能告诉我如何让它发挥作用吗?

Ima*_*ani 11

尝试修改如下代码.

@Html.ValidationMessageFor(model => model.MyImage)
Run Code Online (Sandbox Code Playgroud)

我的建议

您的表单应如下所示.

@using (Html.BeginForm("Acion", "Conroller", FormMethod.Post, 
                                      new { enctype = "multipart/form-data" }))
{
    <input type="file" name="FileInfo" value="File to Upload" />
    @Html.ValidationMessageFor(I => I.FileInfo);
    <button type="submit" name="Upload" value="Upload" />
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

HttpPostedFileBaseModelBinder

*如果HttpPostedFileBase的单个实例作为动作参数或模型中的属性,则映射文件完全由HttpPostedFileBaseModelBinder完成,在这种情况下不使用值提供程序.您可能会想到为什么在这种情况下不使用值提供程序,这是因为源是单一且清晰的,即Request.Files集合.*

在此输入图像描述

模型

public class UploadFileModel
{
    [FileSize(10240)]
    [FileTypes("jpg,jpeg,png")]
    public HttpPostedFileBase FileInfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

FileSizeAttribute

在此输入图像描述

public class FileSizeAttribute : ValidationAttribute
{
    private readonly int _maxSize;

    public FileSizeAttribute(int maxSize)
    {
        _maxSize = maxSize;
    }

    public override bool IsValid(object value)
    {
        if (value == null) return true;

        return _maxSize > (value as HttpPostedFileBase).ContentLength;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format("The file size should not exceed {0}", _maxSize);
    }
}
Run Code Online (Sandbox Code Playgroud)

FileTypesAttribute

public class FileTypesAttribute: ValidationAttribute
{
    private readonly List<string> _types;

    public FileTypesAttribute(string types)
    {
        _types = types.Split(',').ToList();
    }

    public override bool IsValid(object value)
    {
        if (value == null) return true;

        var fileExt = System.IO
                            .Path
                            .GetExtension((value as
                                     HttpPostedFileBase).FileName).Substring(1);
        return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format("Invalid file type. Only the following types {0} 
                                    are supported.", String.Join(", ", _types));
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器动作方法

[HttpPost]
public ActionResult Upload(UploadFileModel fileModel)
{     
    if(ModelState.IsValid)
    {

    }

    return View(fileModel);
}
Run Code Online (Sandbox Code Playgroud)