title: Can I directly stream from HttpResponseMessage to file without going through memory?
body: <p>My program uses <code>HttpClient</code> to send a GET request to a Web API, and this returns a file.</p>
  <p>I now use this code (simplified) to store the file to disc:</p>
  <pre><code>public async Task&lt;bool&gt; DownloadFile()
  {
      var client = new HttpClient();
      var uri = new Uri(&quot;http://somedomain.com/path&quot;);
      var response = await client.GetAsync(uri);
  
      if (response.IsSuccessStatusCode)
      {
          var fileName = response.Content.Headers.ContentDisposition.FileName;
          using (var fs = new FileStream(@&quot;C:\test\&quot; + fileName, FileMode.Create, FileAccess.Write, FileShare.None))
          {
              await response.Content.CopyToAsync(fs);
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  <p>Now, when this code runs, the process loads all of the file into memory. I actually would rather expect the stream gets streamed from the <code>HttpResponseMessage.Content</code> to the <code>FileStream</code>, so that only a small portion of it is held in memory.</p>
  <p>We are planning to use that on large files (&gt; 1GB), so is there a way to achieve that without having all of the file in memory?</p>
  <p>Ideally without manually looping through reading a portion to a <code>byte[]</code> and writing that portion to the file stream until all of the content is written?</p>