Created
June 8, 2014 02:58
-
-
Save angelortiz-dev/b0a86e031ee79bcce48c to your computer and use it in GitHub Desktop.
Validate file extension ( .pdf in example ) in MVC project C#.
This file contains hidden or 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
| using System.ComponentModel.DataAnnotations; | |
| using System.Linq; | |
| using System.Web; | |
| namespace Areas.Cms.Validation | |
| { | |
| public class ValidateExtensionAttribute : ValidationAttribute | |
| { | |
| public override bool IsValid(object value) | |
| { | |
| HttpPostedFileBase file = value as HttpPostedFileBase; | |
| if (file != null) | |
| { | |
| try | |
| { | |
| string[] AllowedFileExtensions = new string[] { ".pdf" }; | |
| var filename = file.FileName; | |
| return AllowedFileExtensions.Contains(filename.Substring(filename.LastIndexOf('.'))); | |
| } | |
| catch { } | |
| return false; | |
| } | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment