“此流不支持搜索操作”

时间:2013-02-21 19:56:04

标签: c# asp.net arrays stream byte

我从网络服务获得了一个流:

Stream resultStream = WebServiceEntities.GetAttachment(attachmentId);

enter image description here

我需要将此流转换为字节数组,但

尝试#1

byte[] resultBytes = null;
using (Stream stream = resultStream)
{
    using (MemoryStream ms = new MemoryStream())
    {
        int count = 0;
        do
        {
            byte[] buf = new byte[1024];
            count = stream.Read(buf, 0, 1024);
            ms.Write(buf, 0, count);
        } while (stream.CanRead && count > 0);
        resultBytes = ms.ToArray();
    }
}

尝试#2:

var memoryStream = new MemoryStream();
resultStream.CopyTo(memoryStream);
byte[] resultBytes = memoryStream.ToArray();

在这两种情况下,(byte [] resultBytes)总是返回一个空的字节数组,有没有人知道导致这种情况发生的原因是什么? Web服务返回的流有什么问题吗?

0 个答案:

没有答案
相关问题