using System;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Drawing;
namespace Ant.Service.Utilities
{
///
/// 文件类型
///
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
BMP = 6677,
PNG = 13780,
RAR = 8297,
jpg = 255216,
exe = 7790,
xml = 6063,
html = 6033,
aspx = 239187,
cs = 117115,
js = 119105,
txt = 210187,
sql = 255254
}
///
/// 图片检测类
///
public static class FileValidation
{
#region 上传图片检测类
///
/// 是否允许
///
public static bool IsAllowedExtension(HttpPostedFile oFile, FileExtension[] fileEx)
{
int fileLen = oFile.ContentLength;
byte[] imgArray = new byte[fileLen];
oFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch { }
br.Close();
ms.Close();
foreach (FileExtension fe in fileEx)
{
if (Int32.Parse(fileclass) == (int)fe) return true;
}
return false;
}
///
/// 上传前的图片是否可靠
///
public static bool IsSecureUploadPhoto(HttpPostedFile oFile)
{
bool isPhoto = false;
string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
isPhoto = true;
break;
}
}
if (!isPhoto)
{
return true;
}
FileExtension[] fe = { FileExtension.BMP, FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
if (IsAllowedExtension(oFile, fe))
return true;
else
return false;
}
///
/// 上传后的图片是否安全
///
/// 物理地址
public static bool IsSecureUpfilePhoto(string photoFile)
{
bool isPhoto = false;
string Img = "Yes";
string fileExtension = System.IO.Path.GetExtension(photoFile).ToLower();
string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
isPhoto = true;
break;
}
}
if (!isPhoto)
{
return true;
}
StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
string strContent = sr.ReadToEnd();
sr.Close();
string str = "request|