1. 程式人生 > >驗證上傳文件真實格式

驗證上傳文件真實格式

zip file name binary bat asp += new for

 1         /// <summary>
 2         /// 驗證上傳文件真實格式
 3         /// </summary>
 4         /// <param name="fileUpload"></param>
 5         /// <returns></returns>
 6         private bool ValidationFile(HttpPostedFile fileUpload)
 7         {
 8             HttpPostedFile file = fileUpload;
9 BinaryReader r = new BinaryReader(fileUpload.InputStream); 10 string bx = " "; 11 byte buffer; 12 try 13 { 14 buffer = r.ReadByte(); 15 bx = buffer.ToString(); 16 buffer = r.ReadByte(); 17 bx += buffer.ToString();
18 } 19 catch (Exception) 20 { 21 Response.Write("文件格式錯誤!"); 22 Response.End(); 23 } 24 25 bool isok = false; 26 foreach (int item in Enum.GetValues(typeof(FileExtension))) 27 { 28 if
(item.ToString() == bx) 29 { 30 isok = true; 31 break; 32 } 33 } 34 35 return isok; 36 } 37 38 /// <summary> 39 /// 上傳文件的真實格式 40 /// </summary> 41 public enum FileExtension 42 { 43 JPG = 255216, 44 GIF = 7173, 45 BMP = 6677, 46 PNG = 13780 47 //COM = 7790, 48 //EXE = 7790, 49 //DLL = 7790, 50 //RAR = 8297, 51 //ZIP = 8075, 52 //XML = 6063, 53 //HTML = 6033, 54 //ASPX = 239187, 55 //CS = 117115, 56 //JS = 119105, 57 //TXT = 210187, 58 //SQL = 255254, 59 //BAT = 64101, 60 //BTSEED = 10056, 61 //RDP = 255254, 62 //PSD = 5666, 63 //PDF = 3780, 64 //CHM = 7384, 65 //LOG = 70105, 66 //REG = 8269, 67 //HLP = 6395, 68 //DOC = 208207, 69 //XLS = 208207, 70 //DOCX = 208207, 71 //XLSX = 208207, 72 //txt:4950 73 }

驗證上傳文件真實格式