真正判斷文件類型

//真正判斷文件類型的關鍵函數
     public static bool IsAllowedExtension(FileUpload hifile)
     {
         System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
         System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
         string fileclass = "";
        //這裏的位長要具體判斷.
         byte buffer;
         try
         {
             buffer = r.ReadByte();
             fileclass = buffer.ToString();
             buffer = r.ReadByte();
             fileclass += buffer.ToString();
 
         }
         catch
         {
 
         }
         r.Close();
         fs.Close();
         if (fileclass == "255216" || fileclass == "7173")//說明:255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
         {
             return true;
         }
         else
         {
             return false;
         }

     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章