file extension data annotations worked only on the string data.i solved this problem with help Validation Attribute that called is custom server side validation.
1.first create a Folder on the root your project called Validation.
2.on this folder create a class called FileExtensionValidation.this like code below:
public class FileExtensionsValidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value,ValidationContext validationContext)
{
if (value != null)
{
HttpPostedFileWrapper file = (HttpPostedFileWrapper)value;
string extention = Path.GetExtension(file.FileName);
if (extention != ".xlsx")
{
return new ValidationResult(".xlsx only");
}
}
return ValidationResult.Success;
}
}
3.on the top page add
Using ProjectName.ValidationFolderName;
4.attach validation above any file upload:
public class FileUploadViewModel:BaseViewModel
{
[Required]
[FileExtensionsValidation]
public HttpPostedFileBase FileUpload { get; set; }
}