Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar requirement which also required me to track progress of the streaming file download. I did it roughly like this:</p> <p><strong><em>server-side</em>:</strong></p> <p>service:</p> <pre class="lang-c# prettyprint-override"><code>public object Get(FooRequest request) { var stream = ...//some Stream return new StreamedResult(stream); } </code></pre> <p>StreamedResult class:</p> <pre class="lang-c# prettyprint-override"><code>public class StreamedResult : IHasOptions, IStreamWriter { public IDictionary&lt;string, string&gt; Options { get; private set; } Stream _responseStream; public StreamedResult(Stream responseStream) { _responseStream = responseStream; long length = -1; try { length = _responseStream.Length; } catch (NotSupportedException) { } Options = new Dictionary&lt;string, string&gt; { {"Content-Type", "application/octet-stream"}, { "X-Api-Length", length.ToString() } }; } public void WriteTo(Stream responseStream) { if (_responseStream == null) return; using (_responseStream) { _responseStream.WriteTo(responseStream); responseStream.Flush(); } } } </code></pre> <p><strong><em>client-side</em>:</strong></p> <pre class="lang-c# prettyprint-override"><code>string path = Path.GetTempFileName();//in reality, wrap this in try... so as not to leave hanging tmp files var response = client.Get&lt;HttpWebResponse&gt;("/foo/bar"); long length; if (!long.TryParse(response.GetResponseHeader("X-Api-Length"), out length)) length = -1; using (var fs = System.IO.File.OpenWrite(path)) fs.CopyFrom(response.GetResponseStream(), new CopyFromArguments(new ProgressChange((x, y) =&gt; { Console.WriteLine("&gt;&gt; {0} {1}".Fmt(x, y)); }), TimeSpan.FromMilliseconds(100), length)); </code></pre> <p>The "CopyFrom" extension method was borrowed directly from the source code file "StreamHelper.cs" in this project here: <a href="http://www.codeproject.com/Articles/356297/Copy-a-Stream-with-Progress-Reporting" rel="nofollow">Copy a Stream with Progress Reporting</a> (Kudos to Henning Dieterichs)</p> <p>And kudos to mythz and any contributor to ServiceStack. Great project!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload