【问题标题】:Send binary file in web service soap response在 Web 服务肥皂响应中发送二进制文件
【发布时间】:2009-06-30 15:10:56
【问题描述】:

我有来自返回二进制文件的 ftp Web 请求的响应流。 我想将二进制数据放入 byte[] 中,然后对该数组进行编码并将其作为 Web 服务响应发送。

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.

我将不胜感激。

谢谢, 莎拉

【问题讨论】:

    标签: c# web-services


    【解决方案1】:

    您可能需要重写 ReadFully 函数以避免像 Length 这样的属性,它需要比未完全接收到完整流时更多地了解流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2011-10-20
      • 2015-01-21
      相关资源
      最近更新 更多