Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from andredublin/ImageType.cs
Created September 25, 2017 01:31
Show Gist options
  • Save LSTANCZYK/8853d01700b159c05449ba9c273875d5 to your computer and use it in GitHub Desktop.
Save LSTANCZYK/8853d01700b159c05449ba9c273875d5 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