Skip to content

Instantly share code, notes, and snippets.

@Dodecahedrane
Last active January 18, 2024 16:46
Show Gist options
  • Save Dodecahedrane/ae0a8e27ce77f50aaf751ef260b424a5 to your computer and use it in GitHub Desktop.
Save Dodecahedrane/ae0a8e27ce77f50aaf751ef260b424a5 to your computer and use it in GitHub Desktop.
File Extension Validation Attribute C# .NET
public class ImageFile : ValidationAttribute
{
private readonly string[] _allowedExtensions = { ".png", ".jpeg", ".gif" };
public override bool IsValid(object value)
{
if (value is IFormFile file)
{
var fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
if (!_allowedExtensions.Contains(fileExtension))
{
return false;
}
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment