2011-07-05 94 views
3

如何將存儲在我的數據庫字段中的二進制數據轉換爲Byte []數組?將二進制轉換爲字節[] array

簡單鑄造二進制爲字節[]不工作

context.Response.BinaryWrite((byte[])images); 
+7

什麼的'確切類型images'? –

+0

你是否得到二進制字符串? –

+0

數據庫對象的數據類型是什麼? – Peter

回答

5

如果圖像是二進制類型的單個記錄,然後調用指定者應該工作

context.Response.BinaryWrite(images.toArray()); 
2
public byte[] FileToByteArray(string _FileName)  
{ 

     byte[] _Buffer = null; 

     try 
     { 
      // Open file for reading 
      System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 

      // attach filestream to binary reader 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 

      // get total byte length of the file 
      long _TotalBytes = new System.IO.FileInfo(_FileName).Length; 

      // read entire file into buffer 
      _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes); 

      // close file reader 
      _FileStream.Close(); 
      _FileStream.Dispose(); 
      _BinaryReader.Close(); 
     } 
     catch (Exception _Exception) 
     { 
      // Error 
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
     } 

     return _Buffer; 
} 
相關問題