我有一个返回流的WCF Web服务。在客户端,当我尝试使用下面的代码读取它然后我在行“Byte [] buffer = new Byte [outputMessage.FileByteStream.Length];”行获得异常说System.Notsupported。请告诉我这里我做错了什么。
FileMetaData metaData = new FileMetaData();
metaData.ProductIDsArray = new string[] { "1", "2" };
metaData.AuthenticationKey = "test";
FileDownloadMessage inputParam = new FileDownloadMessage(metaData);
FileTransferServiceClient obj = new FileTransferServiceClient();
FileDownloadReturnMessage outputMessage = obj.DownloadFile(inputParam);
Byte[] buffer = new Byte[outputMessage.FileByteStream.Length];
int byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
Response.Buffer = false;
Response.Buffer = false;
Response.ContentType = "application/x-zip";
Response.AppendHeader("content-length", buffer.Length.ToString());
Stream outStream = Response.OutputStream;
while (byteRead > 0)
{
outStream.Write(buffer, 0, buffer.Length);
byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
}
outputMessage.FileByteStream.Close();
outStream.Close();
答案 0 :(得分:3)
您正在读取的流不支持获取流的长度(很可能是因为在下载整个文件之前将无法知道长度)。以块的形式读取流 - 类似于while
循环的行为,但是有一个固定大小的缓冲区 - 一旦你为byteRead返回0,你就会知道你已经达到了流末尾。