我从网络服务获得了一个流:
Stream resultStream = WebServiceEntities.GetAttachment(attachmentId);
我需要将此流转换为字节数组,但
尝试#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服务返回的流有什么问题吗?