I have t开发者_StackOverflow中文版he following code to send a (file) stream to a wcf client:
public Stream Download( string path )
{
try
{
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return stream;
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
}
I want to be able to get the length of the sent stream at the client end, but the Stream class does not support this.
What would be the best way of doing it?
Thanks, Tony
[MessageContract]
public class SizedStreamMessage
{
[MessageHeader]
public long streamSize;
[MessageBody] //Has to be just one MessageBody for streaming to work!
public Stream theStream;
}
And then:
[OperationContract]
public SizedStreamMessage Download(string path)
{
//Fill in streamSize...
//Fill in theStream...
}
Obviously it will only work for streams that you can actually get the size for on the server-side without buffering the entire stream (FileStream should work 'cause you can always get the length of the file without actually reading the file).
精彩评论