[C#] 纯文本查看 复制代码
/// 根据文件头判断上传的文件类型
/// </summary>
/// <param name="filePath">filePath是文件的完整路径 </param>
/// <returns>返回true或false</returns>
private bool IsPicture(string filePath)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
string fileClass;
byte buffer;
buffer = reader.ReadByte();
fileClass = buffer.ToString();
buffer = reader.ReadByte();
fileClass += buffer.ToString();
reader.Close();
fs.Close();
if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677")
//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}