2011-07-05 23 views
3

データベースフィールドに格納されているバイナリデータをByte []配列に変換するにはどうすればよいですか?バイナリをバイトに変換する[]配列

画像は、単一のバイナリタイプの記録、その後の呼び出しのtoArrayが動作するはずである場合は

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

画像? –

+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; 
} 
関連する問題