Расширение файла ASP.NET RC2 IFormFile и пользовательская проверка максимального размера файла

Я хочу иметь собственный ненавязчивый валидатор jquery в MVC-6 RC2

Подобно Old Answer, я вижу несколько примеров RC2 в Этот но я не знаю, как реализовать его для файлов.

Вот мой режим просмотра

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

person Amete Blessed    schedule 22.07.2016    source источник
comment
когда-нибудь понять это?   -  person Matt Lengenfelder    schedule 06.06.2017
comment
В итоге я решил свою проблему, используя атрибуты. Я опубликую ответ в ближайшее время.   -  person Amete Blessed    schedule 06.06.2017


Ответы (1)


В итоге я решил свою проблему, используя атрибуты

Вот как я его создал. (Мой размер и расширения являются статическими в нашем приложении, поэтому я жестко запрограммировал их в 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;
        }
    }

Затем внутри моей модели просмотра я использую атрибут следующим образом

public class FileUploadViewModel
    {
         [FileType("invalid format", "invalid size")]
        [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; }    
    }

Затем внутри 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");

    });
person Amete Blessed    schedule 06.06.2017