2013-08-24 37 views
1

我已經嘗試了RegularExpression數據註釋的很多正則表達式來檢查文件擴展名是否爲圖像,並且它總是返回false,例如我也試過FileExtension屬性,但它在jquery.validation上創建了一個錯誤。我使用ASP.NET MVC 4剃刀DataAnnotation對於文件輸入,正則表達式總是返回false

[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> 

有人能告訴我如何使它發揮作用?

+0

什麼是錯誤 – ps2goat

+0

它總是顯示爲一個錯誤,所以我不能使用MVC 4剃鬚刀後我的形式進出口 – ONYX

+0

我不知道我不知道正則表達式的語法非常好所以我不知道 – ONYX

回答

1

爲字段MyImage定義了RegularExpression,但您的@ValidationMessageFor正在爲DepartmentImage進行驗證。

這應該是

@Html.TextBoxFor(x => x.MyImage, new { type = "file" }) @Html.ValidationMessageFor(model => model.MyImage)

10

嘗試修改如下面的代碼。

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

enter image description here

HttpPostedFileBaseModelBinder

*當你HttpPostedFileBase的單一實例作爲一個動作參數或模型中的屬性,則映射文件由是完全做得到HttpPostedFileBaseModelBinder並且在這種情況下不使用值提供程序。您可能會想,爲什麼在這種情況下沒有使用值提供程序,這是因爲源代碼是單一且清晰的,即Request.Files集合。*

enter image description here

型號

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

enter image description here

FileSizeAttribute

enter image description here

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

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

控制器的操作方法

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

    } 

    return View(fileModel); 
} 
+0

不錯,我會給出一個去看看我想出了什麼 – ONYX

+0

這一切看起來不錯,但我只是使用一個字符串字段的MyImage實際上被稱爲DepartmentImage可以添加代碼驗證字符串,而不是HttpPostedFileBase,但離開請寫下你寫的代碼。 – ONYX

相關問題