Last active
January 18, 2024 16:46
-
-
Save Dodecahedrane/ae0a8e27ce77f50aaf751ef260b424a5 to your computer and use it in GitHub Desktop.
File Extension Validation Attribute C# .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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