C# 如何使用DataAnnotation使用多个文件扩展名?
例如,以下代码可以正常工作:C# 如何使用DataAnnotation使用多个文件扩展名?,c#,data-annotations,file-extension,C#,Data Annotations,File Extension,例如,以下代码可以正常工作: [Required(ErrorMessage = "Choose an image"), FileExtensions(Extensions = "jpg", ErrorMessage = "Error")] public HttpPostedFileBase BannerData { get; set; } 但我需要更多的扩展。我试着添加了几种格式,但不起作用: “jpg,gif,png”或“*.jpg,*.gif,*.png”或“gif |*.gif | j
[Required(ErrorMessage = "Choose an image"),
FileExtensions(Extensions = "jpg", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }
但我需要更多的扩展。我试着添加了几种格式,但不起作用:
“jpg,gif,png”
或“*.jpg,*.gif,*.png”
或“gif |*.gif | jpg |*.jpg;*.jpg | png |*.png”
等。是否可以使用更多的文件扩展名?使用以下方法:
[Required(ErrorMessage = "Choose an image"),
FileExtensions(Extensions = "jpg,jpeg,gif,png", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }
扩展需要用逗号分隔,并且没有空格(空格将被视为扩展的一部分),因此这应该可以:
[Required(ErrorMessage = "Choose an image"),
FileExtensions(Extensions = "jpg,gif,png", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }
这仅适用于一个文件扩展名,适用于“HttpPostedFileBase”和“string”属性类型,并且仅适用于服务器端(因此它在上传文件后告知结果!): 因此,如果你这样做,它将永远不会起作用:
FileExtensions(Extensions = "jpg,gif,png", ErrorMessage = "Error")]
FileExtensions(Extensions = "jpg|gif|png", ErrorMessage = "Error")]
FileExtensions(Extensions = "jpg gif png", ErrorMessage = "Error")]
etc...
使用DataAnnotation使用多个文件扩展名的真正方法是创建一个简单的自定义验证属性强>
步骤#1:在任何地方创建一个文件夹,称它为任何东西,创建一个类名它什么的哈哈
YOUR_SOLUTION_NAME\Models\CustomValidation\ValidImageFileAttribute.cs
第2步:让它看起来像这样,您可以将isValid方法中的代码更改为您需要的任何代码
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;
namespace YOUR_SOLUTION_NAME.Models.CustomValidation
{
public class ValidImageFileAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
return false;
string[] _validExtensions = { "JPG", "JPEG", "BMP", "GIF", "PNG" };
var file = (HttpPostedFileBase)value;
var ext = Path.GetExtension(file.FileName).ToUpper().Replace(".", "");
return _validExtensions.Contains(ext) && file.ContentType.Contains("image");
}
}
}
步骤3:在模型中使用自定义验证
namespace YOUR_SOLUTION_NAME.Models
{
public class testModel
{
[Required(ErrorMessage = "Please attached the personal photo")]
[ValidImageFile(ErrorMessage = "The file is not a valid image file")]
public HttpPostedFileBase File { get; set; }
}
}
步骤4:在视图中使用模型
@model FacilityMvcApp.Models.testModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.TextBoxFor(Model => Model.File, new { type = "file" })
@Html.ValidationMessageFor(model => model.File ,null, new { @class = "text-danger" })
</div>
</div>
}
@model FacilityMvcApp.Models.testModel
@使用(Html.BeginForm(null,null,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.TextBoxFor(Model=>Model.File,新的{type=“File”})
@Html.ValidationMessageFor(model=>model.File,null,新{@class=“text danger”})
}
步骤#5:要了解有关自定义验证的更多信息请参见此问题:可能没有空格
jpg,gif,png
@DavidG哦,没有空格真的可以用,非常感谢
@model FacilityMvcApp.Models.testModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.TextBoxFor(Model => Model.File, new { type = "file" })
@Html.ValidationMessageFor(model => model.File ,null, new { @class = "text-danger" })
</div>
</div>
}