2011-07-05 6 views
3

데이터베이스 필드에 저장된 바이너리 데이터를 Byte [] 배열로 변환하려면 어떻게해야합니까?바이너리를 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; 
}