62,266
社区成员
发帖
与我相关
我的任务
分享
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
SWF = 6787,
RAR = 8297,
ZIP = 8075,
_7Z = 55122,
BMP=6677,
EXE=7790,
XML=6063,
HTML=6033,
ASPX=239187,
CS=117115,
JS=119105,
TXT=102100
}
public static bool IsAllowedExtension(HttpPostedFile fu, FileExtension[] fileEx)
{
int fileLen = fu.ContentLength;
byte[] imgArray = new byte[fileLen];
fu.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;
}
if (postedfile != null)
{
if (!Tools.Verification.IsAllowedExtension(postedfile, fe))
{
if (target != "")
{
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(target)))
{
System.IO.File.Delete(HttpContext.Current.Server.MapPath(target));
err = "文件非法!!!";
}
}
}
}
private static string[] FileExtension=new string[]{"application/msword","application/pdf","application/vnd.ms-powerpoint","application/x-shockwave-flash","application/zip","image/bmp","image/gif","image/jpeg","text/plain",};
public static bool IsAllowedExtension(HttpPostedFile fu)
{
for (int i = 0; i < FileExtension.Length; i++)
{
if (fu.ContentType.StartsWith(FileExtension[i]))
{
return true;
}
}
return false;
}