Skip to content

Instantly share code, notes, and snippets.

@angelortiz-dev
Created June 8, 2014 02:58
Show Gist options
  • Select an option

  • Save angelortiz-dev/b0a86e031ee79bcce48c to your computer and use it in GitHub Desktop.

Select an option

Save angelortiz-dev/b0a86e031ee79bcce48c to your computer and use it in GitHub Desktop.
Validate file extension ( .pdf in example ) in MVC project C#.
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