Asp.net mvc ASP.NET RC2文件扩展名和文件最大大小自定义验证
我希望在MVC-6RC2中有定制的jquery不引人注目的验证器 类似于我在中看到的一些RC2示例,但我不知道如何为文件实现它 这是我的视图模式Asp.net mvc ASP.NET RC2文件扩展名和文件最大大小自定义验证,asp.net-mvc,asp.net-core-mvc,unobtrusive-validation,.net-core-rc2,Asp.net Mvc,Asp.net Core Mvc,Unobtrusive Validation,.net Core Rc2,我希望在MVC-6RC2中有定制的jquery不引人注目的验证器 类似于我在中看到的一些RC2示例,但我不知道如何为文件实现它 这是我的视图模式 public class FileUploadViewModel { //TODO [FileType(Validtype="jpeg,png,jif", MaxSize=112222)]// this is what I want [Required(ErrorMessage = "Please select
public class FileUploadViewModel
{
//TODO [FileType(Validtype="jpeg,png,jif", MaxSize=112222)]// this is what I want
[Required(ErrorMessage = "Please select a file")]
public IFormFile File { get; set; }
[Required(ErrorMessage = "Please select link")]
public string FileName { get; set; }
public string ExternalLink { get; set; }
public string Description { get; set; }
}
我最终通过使用属性解决了我的问题 下面是我如何创建它的。(通过我们的应用程序,我的大小和扩展是静态的,这就是我在FileTypeAttribute中硬编码它的原因,但是如果需要,您可以将其设置为动态的,并将其传递给属性构造函数
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FileTypeAttribute : ValidationAttribute, IClientModelValidator
{
private const int MaxSize = 1048576;
private const string _DefaultErrorMessage = "Only the following file types are allowed: {0}";
private IEnumerable<string> _ValidTypes { get; set; }
public string ValidTypes { get; set; }
public string ErrorMessageExtension { get; set; }
public string ErrorMessageSize { get; set; }
public FileTypeAttribute(string errorExtension, string errorSize)
{
ILang lang = ((ContextServiceImpl)ContextService.Instance).HttpContext.RequestServices.GetService(typeof(ILang)) as ILang;
ErrorMessageExtension = lang[errorExtension];
ErrorMessageSize = lang[errorSize];
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
IFormFile file = value as IFormFile;
if (file != null)
{
if (!_ValidTypes.Any(e => file.FileName.EndsWith(e)))
{
return new ValidationResult(ErrorMessageExtension);
}
if (file.Length > MaxSize)
{
return new ValidationResult(ErrorMessageSize);
}
}
return ValidationResult.Success;
}
public void AddValidation(ClientModelValidationContext context)
{
MergeAttribute(context.Attributes, "data-val", "true");
var errorMessage = FormatErrorMessage(context.ModelMetadata.GetDisplayName());
MergeAttribute(context.Attributes, "data-val-fileextensions", ErrorMessageExtension);
MergeAttribute(context.Attributes, "data-val-maxfilesize", ErrorMessageSize);
}
private bool MergeAttribute(
IDictionary<string, string> attributes, string key, string value)
{
if (attributes.ContainsKey(key))
{
return false;
}
attributes.Add(key, value);
return true;
}
}
然后在javascript内部执行此操作
$.validator.addMethod("fileextensions",
function (value, element, param) {
var fileType = $(element)[0].files[0].type;
var fileTypes = ["image/jpeg", "image/pjpeg", "image/gif", "image/bmp", "image/png", "image/x-png", "image/tiff"]
var validExtension = $.inArray(type, fileTypes) !== -1;
return validExtension;
});
$.validator.addMethod("maxfilesize",
function (value, element, param) {
var fileSize = $(element)[0].files[0].size;
var maxSize = 1048576;
var validSize = fileSize < maxSize;
return validSize;
});
$.validator.unobtrusive.adapters.add('fileextensions', [], function (options) {
var params = {
fileextensions: $(options.element).data("val-fileextensions").split(',')
};
options.rules['fileextensions'] = params;
options.messages['fileextensions'] = $(options.element).data("val-fileextensions");
});
$.validator.unobtrusive.adapters.add('maxfilesize', [], function (options) {
options.rules['maxfilesize'] = [];
options.messages['maxfilesize'] = $(options.element).data("val-maxfilesize");
});
$.validator.addMethod(“文件扩展名”,
函数(值、元素、参数){
var fileType=$(元素)[0]。文件[0]。类型;
var fileTypes=[“image/jpeg”、“image/pjpeg”、“image/gif”、“image/bmp”、“image/png”、“image/x-png”、“image/tiff”]
var validExtension=$.inArray(类型,文件类型)!=-1;
回程张力;
});
$.validator.addMethod(“maxfilesize”,
函数(值、元素、参数){
var fileSize=$(元素)[0]。文件[0]。大小;
var maxSize=1048576;
var validSize=fileSize
我最终通过使用属性解决了问题
下面是我如何创建它的。(我的大小和扩展在应用程序中是静态的,这就是我在FileTypeAttribute中硬编码它的原因,但是如果需要,您可以将它设置为动态的,并将其传递给属性构造函数。)
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FileTypeAttribute : ValidationAttribute, IClientModelValidator
{
private const int MaxSize = 1048576;
private const string _DefaultErrorMessage = "Only the following file types are allowed: {0}";
private IEnumerable<string> _ValidTypes { get; set; }
public string ValidTypes { get; set; }
public string ErrorMessageExtension { get; set; }
public string ErrorMessageSize { get; set; }
public FileTypeAttribute(string errorExtension, string errorSize)
{
ILang lang = ((ContextServiceImpl)ContextService.Instance).HttpContext.RequestServices.GetService(typeof(ILang)) as ILang;
ErrorMessageExtension = lang[errorExtension];
ErrorMessageSize = lang[errorSize];
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
IFormFile file = value as IFormFile;
if (file != null)
{
if (!_ValidTypes.Any(e => file.FileName.EndsWith(e)))
{
return new ValidationResult(ErrorMessageExtension);
}
if (file.Length > MaxSize)
{
return new ValidationResult(ErrorMessageSize);
}
}
return ValidationResult.Success;
}
public void AddValidation(ClientModelValidationContext context)
{
MergeAttribute(context.Attributes, "data-val", "true");
var errorMessage = FormatErrorMessage(context.ModelMetadata.GetDisplayName());
MergeAttribute(context.Attributes, "data-val-fileextensions", ErrorMessageExtension);
MergeAttribute(context.Attributes, "data-val-maxfilesize", ErrorMessageSize);
}
private bool MergeAttribute(
IDictionary<string, string> attributes, string key, string value)
{
if (attributes.ContainsKey(key))
{
return false;
}
attributes.Add(key, value);
return true;
}
}
然后在javascript内部执行此操作
$.validator.addMethod("fileextensions",
function (value, element, param) {
var fileType = $(element)[0].files[0].type;
var fileTypes = ["image/jpeg", "image/pjpeg", "image/gif", "image/bmp", "image/png", "image/x-png", "image/tiff"]
var validExtension = $.inArray(type, fileTypes) !== -1;
return validExtension;
});
$.validator.addMethod("maxfilesize",
function (value, element, param) {
var fileSize = $(element)[0].files[0].size;
var maxSize = 1048576;
var validSize = fileSize < maxSize;
return validSize;
});
$.validator.unobtrusive.adapters.add('fileextensions', [], function (options) {
var params = {
fileextensions: $(options.element).data("val-fileextensions").split(',')
};
options.rules['fileextensions'] = params;
options.messages['fileextensions'] = $(options.element).data("val-fileextensions");
});
$.validator.unobtrusive.adapters.add('maxfilesize', [], function (options) {
options.rules['maxfilesize'] = [];
options.messages['maxfilesize'] = $(options.element).data("val-maxfilesize");
});
$.validator.addMethod(“文件扩展名”,
函数(值、元素、参数){
var fileType=$(元素)[0]。文件[0]。类型;
var fileTypes=[“image/jpeg”、“image/pjpeg”、“image/gif”、“image/bmp”、“image/png”、“image/x-png”、“image/tiff”]
var validExtension=$.inArray(类型,文件类型)!=-1;
回程张力;
});
$.validator.addMethod(“maxfilesize”,
函数(值、元素、参数){
var fileSize=$(元素)[0]。文件[0]。大小;
var maxSize=1048576;
var validSize=fileSize
有没有想过?我最终用属性解决了问题。我会很快发布答案。有没有想过?我最终用属性解决了问题。我会很快发布答案。