FileTypeCheck.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
public static class FileTypeCheck
{
//真正判断文件类型的关键函数
public static bool IsAllowedExtension(HttpPostedFile hifile1,string[] headNumArr)
{
HttpPostedFile hifile = hifile1;
//System.IO.FileStream fs = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(hifile.InputStream);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{ }
r.Close();
//fs.Close();
bool ret = false;
foreach (string s in headNumArr) {
if (fileclass == s) {
ret = true;
break;
}
}
//if (fileclass == "8075")
//{
// MessageBox.Show("xlsx,zip,pptx,mmap,zip");
//}
//if (fileclass == "208207")
//{
// MessageBox.Show("xls.doc.ppt");
//}
//if (fileclass == "4944")
//{
// MessageBox.Show("csv");
//}
//if (fileclass == "255216" || fileclass == "7173")
////说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
//{
// return true;
//}
//else
//{
// return false;
//}
return ret;
}
}