3

For example, the following code works fine:

[Required(ErrorMessage = "Choose an image"), 
FileExtensions(Extensions = "jpg", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }

But I need more extensions. I tried just add several formats, but it's not work:

"jpg, gif, png" or "*.jpg, *.gif, *.png" or "GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png" and so on. Is it possible to use more file extensions?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mia
  • 433
  • 4
  • 11
  • 25

3 Answers3

3

The extensions need to be comma separated and to not have spaces (spaces will be treated as part of the extension), so this should work:

[Required(ErrorMessage = "Choose an image"), 
FileExtensions(Extensions = "jpg,gif,png", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

User it this way:

[Required(ErrorMessage = "Choose an image"), 
FileExtensions(Extensions = "jpg,jpeg,gif,png", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }
iDraGoN
  • 119
  • 8
0

This only works with one single file extension and it works with "HttpPostedFileBase" and with "string" property types and only works on server side (so it tells the results after the file is uploaded !):

FileExtensions(Extensions = "jpg", ErrorMessage = "Error")]
public HttpPostedFileBase BannerData { get; set; }

So if you do something like this, it will never work:

FileExtensions(Extensions = "jpg,gif,png", ErrorMessage = "Error")]
FileExtensions(Extensions = "jpg|gif|png", ErrorMessage = "Error")]
FileExtensions(Extensions = "jpg gif png", ErrorMessage = "Error")]
etc...

The true way to use multiple file extensions using DataAnnotations is by creating a simple Custom Validation Attribute !

Step #1 : Create a folder anywhere, call it anything, create a class name it something haha

YOUR_SOLUTION_NAME\Models\CustomValidation\ValidImageFileAttribute.cs

Step #2 : Make it look like this, You can change the code inside the isValid method to whatever you need

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

Step #3 : Use the custom validation in your model

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

    }
}

Step #4 : use the model in the view

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

Step #5: watch this tutorial to learn more about custom validation

Adel Mourad
  • 1,351
  • 16
  • 13