Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created January 17, 2013 22:27
Show Gist options
  • Select an option

  • Save andredublin/4560432 to your computer and use it in GitHub Desktop.

Select an option

Save andredublin/4560432 to your computer and use it in GitHub Desktop.
C# class to check image file type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace MvcMovie.Service.AppService
{
public class ImageType : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string postedFileName = ((HttpPostedFileBase)value).FileName.ToLower(),
pattern = @".*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg])";
if ( ! Regex.IsMatch(postedFileName, pattern))
{
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment