Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataAnnotation Regular Expression always returns false for file input

I've tried so many regular expressions for the RegularExpression data annotation to check if the file extension is an image and it always returns false e.g. I've also tried FileExtension attribute but it creates an error on the jquery.validation. I'm using 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; }

and this is my markup

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

Could someone show me how to make it work?

like image 392
ONYX Avatar asked Dec 07 '25 03:12

ONYX


1 Answers

Try modifying the code like below.

@Html.ValidationMessageFor(model => model.MyImage)

My Suggestion

Your form should be like below.

@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

*When you have a single instance of HttpPostedFileBase as an action parameter or a property in model then mapping the file is completely done by the HttpPostedFileBaseModelBinder and no value providers are used in this case. You may think why no value providers are used in this case, it's because the source is single and clear i.e. Request.Files collection.*

enter image description here

Model

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

Controller Action Method

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

    }

    return View(fileModel);
}
like image 146
Imad Alazani Avatar answered Dec 08 '25 23:12

Imad Alazani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!