WCF exception while getting stream length
I have a WCF web service that returns a stream. At the client side when i try to read it using below code then i get an exception at line " Byte[] buffer = new Byte[outputMessage.FileByteStream.Length];" saying System.Notsupported. Please advise me on what am i doing wrong here.
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();
The stream that you're reading from does not support getting the length of the stream (most likely 'cause the length will not be known until the entire file has been downloaded). Read the stream in chunks - similar to how the while
loop is doing, but have a fixed size buffer - once you get 0 returned for byteRead you'll know you've hit the end-of-stream.
精彩评论