码迷,mamicode.com
首页 > Web开发 > 详细

判断上传文件是否是图片文件

时间:2015-06-25 19:17:55      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

方法一:用image对象判断是否为图片

技术分享
/// <summary>
/// 判断文件是否为图片
/// </summary>
/// <param name="path">文件的完整路径</param>
/// <returns>返回结果</returns>
public Boolean IsImage(string path)
{
try
{
 System.Drawing.Image img = System.Drawing.Image.FromFile(path);
 return true;
}
catch (Exception e)
{
 return false;
}
}
View Code


方法二,判断文件头

技术分享
/// <summary>
/// 根据文件头判断上传的文件类型
/// </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;
}
}
public enum FileExtension
{
    JPG = 255216,
    GIF = 7173,
    BMP = 6677,
    PNG = 13780,
    COM = 7790,
    EXE = 7790,
    DLL = 7790,
    RAR = 8297,
    ZIP = 8075,
    XML = 6063,
    HTML = 6033,
    ASPX = 239187,
    CS = 117115,
    JS = 119105,
    TXT = 210187,
    SQL = 255254,
    BAT = 64101,
    BTSEED = 10056,
    RDP = 255254,
    PSD = 5666,
    PDF = 3780,
    CHM = 7384,
    LOG = 70105,
    REG = 8269,
    HLP = 6395,
    DOC = 208207,
    XLS = 208207,
    DOCX = 208207,
    XLSX = 208207,
}
View Code

 

判断上传文件是否是图片文件

标签:

原文地址:http://www.cnblogs.com/ZJ199012/p/4600444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!