2009-06-30 13 views
2

私はバイナリファイルを返すftp Webリクエストからの応答ストリームを持っています。 バイナリデータをbyte []に​​変換し、その配列をエンコードしてWebサービスレスポンスとして送信したいとしました。WebサービスのSOAP応答でバイナリファイルを送信

Stream responseStream = webResponse.GetResponseStream(); 
byte[] byteToEncode= ReadFully(responseStream,1024); 
String str=Convert.ToBase64String(byteToEncode); 

私は[]

/// <summary> 
/// Reads data from a stream until the end is reached. The 
/// data is returned as a byte array. An IOException is 
/// thrown if any of the underlying IO calls fail. 
/// </summary> 
/// <param name="stream">The stream to read data from</param> 
/// <param name="initialLength">The initial buffer length</param> 

public static byte[] ReadFully (Stream stream, int initialLength) 
{ 
    // If we've been passed an unhelpful initial length, just use 32K. 
    if (initialLength < 1) 
    { 
     initialLength = 32768; 
    } 

    byte[] buffer = new byte[initialLength]; 
    int read=0; 

    int chunk; 
    while ((chunk = stream.Read(buffer, read, buffer.Length-read)) > 0) 
    { 
     read += chunk; 

     // If we've reached the end of our buffer, check to see if there's 
     // any more information 

     if (read == buffer.Length) 
     { 
      int nextByte = stream.ReadByte(); 

      // End of stream? If so, we're done 

      if (nextByte==-1) 
      { 
       return buffer; 
      } 

      // Nope. Resize the buffer, put in the byte we've just 
      // read, and continue 
      byte[] newBuffer = new byte[buffer.Length*2]; 

      Array.Copy(buffer, newBuffer, buffer.Length); 
      newBuffer[read]=(byte)nextByte; 
      buffer = newBuffer; 
      read++; 
     } 
    } 
    // Buffer is now too big. Shrink it. 

    byte[] ret = new byte[read]; 
    Array.Copy(buffer, ret, read); 
    return ret; 
} 

バイトにストリームを変換するために、私はオンラインで見つけるその機能を使用しかし、私は例外だと言う:

This stream does not support seek operations. 

私は任意の助けをいただければ幸いです。

おかげで、 サラ

答えて

0
あなたはおそらく完全なストリームが完全にされていない場合に可能であるよりも、ストリームについての詳細を知る必要がある長さのような性質を回避するのreadFully機能を再書き込みする必要があります

受け取った。

関連する問題