Stream wrapper to make Stream seekable?

后端 未结 5 782
一整个雨季
一整个雨季 2020-12-05 10:37

I have a readonly System.IO.Stream implementation that is not seekable (and its Position always returns 0). I need to send it to a consumer that do

5条回答
  •  长情又很酷
    2020-12-05 11:08

    If using System.Net.WebClient, instead of using OpenRead() which returns a Stream use webClient.DownloadData("https://your.url") to get a byte array which you can then turn into a MemoryStream. Here is an example:

    byte[] buffer = client.DownloadData(testBlobFile);
    using (var stream = new MemoryStream(buffer))
    {
        ... your code using the stream ...
    }
    

    Obviously this downloads everything before the Stream is created, so it may defeat the purpose of using a Stream.

提交回复
热议问题