2009-06-30 9 views
2

Ich habe Antwort-Stream von einem FTP-Web-Anfrage, die Binärdatei zurückgibt. Ich wollte die Binärdaten in Byte [] erhalten und dann dieses Array codieren und es als Web-Service-Antwort senden.Binäre Datei in Web-Service-Soap-Antwort senden

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

habe ich diese Funktion ich online den Strom in byte []

/// <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; 
} 

Aber ich habe eine Ausnahme, sagt zu konvertieren gefunden:

This stream does not support seek operations. 

Ich würde jede Hilfe dankbar.

Danke, Sarah

Antwort

0

Sie müssen wahrscheinlich die readfully Funktion neu schreiben Eigenschaften wie Länge zu vermeiden, die mehr über den Stream wissen müssen, als dies möglich ist, wenn der volle Strom nicht vollständig gewesen ist empfangen.

Verwandte Themen