csharp C#类检查图像文件类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#类检查图像文件类型相关的知识,希望对你有一定的参考价值。

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;
		}
	}
}

以上是关于csharp C#类检查图像文件类型的主要内容,如果未能解决你的问题,请参考以下文章