2

ファイル拡張子が画像であるかどうかを確認するためにデータ注釈の非常に多くの正規表現を試しました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; }

これが私のマークアップです

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

誰かがそれを機能させる方法を教えてもらえますか?

4

2 に答える 2

11

以下のようにコードを変更してみてください。

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

ここに画像の説明を入力

HttpPostedFileBaseModelBinder

*アクション パラメーターまたはモデルのプロパティとしてHttpPostedFileBaseの単一インスタンスがある場合、ファイルのマッピングはHttpPostedFileBaseModelBinderによって完全に行われ、この場合は値プロバイダーは使用されません。この場合、なぜ値プロバイダーが使用されていないのかと思うかもしれません。それは、ソースが単一で明確であるためです。つまり、Request.Files コレクションです。

ここに画像の説明を入力

モデル

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

ここに画像の説明を入力

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

ファイルの種類属性

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

コントローラ アクション メソッド

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

    }

    return View(fileModel);
}
于 2013-08-24T06:26:04.513 に答える