tags:

views:

14

answers:

1

Hi,

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();
+2  A: 

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.

Will A
thanks for the reply. What size should i give to bytearray? I won't know the size of the file i will receive it can be 10 mb it can be 500 mb. Please advise
Amit