我从网络服务中得到一个流:
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 服务返回的 Stream 有问题吗?