title: WebAPI StreamContent vs PushStreamContent
body: <p>I'm implementing a <a href="https://github.com/bUKaneer/bluimp-jquery-file-upload-with-mvc4-and-webapi" rel="noreferrer">MVC4 + WebAPI version of the BluImp jQuery File Upload</a> all works well with my initial attempt but Im trying to ensure the best use of memory whilst downloading very large files (~2GB). </p>
  
  <p>I've read <a href="http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/" rel="noreferrer">Filip Woj's article on PushStreamContent</a> and implemented it as best I can (removing the async parts - perhaps this is the problem?). When Im running tests and watching TaskManager Im not seeing much difference memory usage wise and Im trying to understand the difference between how the responses are handled.</p>
  
  <p>Here's my StreamContent version:</p>
  
  <pre><code>private HttpResponseMessage DownloadContentNonChunked()
  {
      var filename = HttpContext.Current.Request["f"];
      var filePath = _storageRoot + filename;
      if (File.Exists(filePath))
      {
          HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
          response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
          response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
          response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
          {
              FileName = filename
          };
          return response;
      }
      return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
  }
  </code></pre>
  
  <p>And here's my PushStreamContent version:</p>
  
  <pre><code>public class FileDownloadStream
  {
      private readonly string _filename;
  
      public FileDownloadStream(string filePath)
      {
          _filename = filePath;
      }
  
      public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
      {
          try
          {
              var buffer = new byte[4096];
  
              using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
              {
                  var length = (int)video.Length;
                  var bytesRead = 1;
  
                  while (length &gt; 0 &amp;&amp; bytesRead &gt; 0)
                  {
                      bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                      outputStream.Write(buffer, 0, bytesRead);
                      length -= bytesRead;
                  }
              }
          }
          catch (HttpException ex)
          {
              return;
          }
          finally
          {
              outputStream.Close();
          }
      }
  }
  
  private HttpResponseMessage DownloadContentChunked()
  {
      var filename = HttpContext.Current.Request["f"];
      var filePath = _storageRoot + filename;
      if (File.Exists(filePath))
      {
          var fileDownload = new FileDownloadStream(filePath);
          var response = Request.CreateResponse();
          response.Content = new PushStreamContent(fileDownload.WriteToStream, new MediaTypeHeaderValue("application/octet-stream"));
          response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
          {
              FileName = filename
          };
          return response;
      }
      return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
  }
  </code></pre>
  
  <p>My question is why am I not seeing much difference in memory usage between the two approaches? Additionally Ive downloaded the PDB for the StreamContent type and can see references to buffer sizes and such forth (see below) so I'd like to know exactly what PushStreamContent is doing above and beyond StreamContent. Ive check the Type info on MSDN but the article were a little light on explanation!</p>
  
  <pre><code>namespace System.Net.Http
  {
    /// &lt;summary&gt;
    /// Provides HTTP content based on a stream.
    /// &lt;/summary&gt;
    [__DynamicallyInvokable]
    public class StreamContent : HttpContent
    {
      private Stream content;
      private int bufferSize;
      private bool contentConsumed;
      private long start;
      private const int defaultBufferSize = 4096;
  
      /// &lt;summary&gt;
      /// Creates a new instance of the &lt;see cref="T:System.Net.Http.StreamContent"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="content"&gt;The content used to initialize the &lt;see cref="T:System.Net.Http.StreamContent"/&gt;.&lt;/param&gt;
      [__DynamicallyInvokable]
      [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
      public StreamContent(Stream content)
        : this(content, 4096)
      {
      }
  </code></pre>