Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from StevenSwann/AllowedFileExtentions
Created September 25, 2017 01:30
Show Gist options
  • Save LSTANCZYK/e0e42523aa0e41736c73bc286f7b191e to your computer and use it in GitHub Desktop.
Save LSTANCZYK/e0e42523aa0e41736c73bc286f7b191e to your computer and use it in GitHub Desktop.
Data Annotation Custom Validation Allowed File Extentions
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AllowedFileExtensionsAttribute : ValidationAttribute
{
private List<string> AllowedExtensions { get; set; }
public AllowedFileExtensionsAttribute(string fileExtensions)
{
AllowedExtensions = fileExtensions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
public override bool IsValid(object value)
{
HttpPostedFileBase file = value as HttpPostedFileBase;
if (file != null)
{
var fileName = file.FileName;
return AllowedExtensions.Any(y => fileName.EndsWith(y));
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment