标签:
方法一:用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; } }
方法二,判断文件头
/// <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, }
标签:
原文地址:http://www.cnblogs.com/ZJ199012/p/4600444.html