Asp.net mvc 对于文件输入,DataAnnotation正则表达式始终返回false

Asp.net mvc 对于文件输入,DataAnnotation正则表达式始终返回false,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我已经为RegularExpression数据注释尝试了很多正则表达式,以检查文件扩展名是否为图像,并且它总是返回false。例如,我也尝试了FileExtension属性,但它在jquery.validation上创建了一个错误。我正在使用ASP.NETMVC4Razor [RegularExpression(@"^.*\.(jpg|gif|jpeg|png|bmp)$", ErrorMessage = "Please use an image with an extension of .j

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

[RegularExpression(@"^.*\.(jpg|gif|jpeg|png|bmp)$", 
ErrorMessage = "Please use an image with an extension of .jpg, .png, .gif, .bmp")]
public string MyImage { get; set; }
这是我的标记

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

@Html.TextBoxFor(x=>x.DepartmentImage,新的{type=“file”})
@Html.ValidationMessage(“DepartmentImageError”)
@Html.ValidationMessageFor(model=>model.DepartmentImage)

有人能告诉我如何使它工作吗?

有一个为字段
MyImage
定义的正则表达式,但是您的
@ValidationMessageFor
为部门图像验证

这应该是

@Html.TextBoxFor(x=>x.MyImage,新的{type=“file”})

@Html.ValidationMessageFor(model=>model.MyImage)

尝试修改如下代码

@Html.ValidationMessageFor(model => model.MyImage)
我的建议

您的表单应如下所示。

@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" />
}

文件大小属性

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);
    }
}
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));
    }
}

文件类型属性

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);
    }
}
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));
    }
}

它总是显示为错误,因此我无法使用MVC4发布我的表单即时消息RazorI不知道我不太了解正则表达式语法,因此我不确定是否总是会出现某种类型的javascript错误,或者以某种类似的方式告诉我为什么某些东西不起作用。您需要在浏览器中启用它和/或在代码中实现日志记录。反斜杠转义“.”,字符串前面的@表示字符串将保持原样,不会进行C代码替换。例如,@“\n”不会插入新行。正则表达式本身适用于我。很好,我会试一试,看看我想出了什么,看起来都不错,但我只是为MyImage使用了一个字符串字段,它实际上被称为DepartmentImage。你可以添加验证字符串的代码而不是HttpPostedFileBase,但请留下你编写的代码。