2011-07-05 92 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; 
} 
相关问题