Search code examples
c#.netasync-awaitstreamdotnet-httpclient

Reduce memory footprint while downloading a file with HTTPCLient


I was wondering if there's a way I can avoid of getting all the buffer, then writing them to a file using HttpClient and File.WriteAllBytes.

Here's the code snippet I use

public async Task<byte[]> DownloadAsByteArray(string filename)
{
    _logger.LogDebug($"Start downloading {filename} file at {DateTime.Now}");
    
    var result = await _httpClient.GetByteArrayAsync(filename);
    
    return result;
}
var bytes = await _downloadFileService.DownloadAsByteArray(fileDownload);

await File.WriteAllBytesAsync(fullFlePathName, bytes);

For quite huge file, the application memory grows really fast.


Solution

  • How about using GetAsync instead of GetByteArrayAsync and using Content's CopyTo?

    var response = await _httpClient.GetAsync(uri);
    using var fs = new FileStream(...);
    await response.Content.CopyToAsync(fs);
    

    Or using GetStreamAsync

    using var responseStream = await _httpClient.GetStreamAsync(uri);
    using var fs = new FileStream(...);
    await responseStream.CopyToAsync(fs);