3

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.

2

1 Answer 1

6

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);
2
  • 1
    are there are same efficient or GetStreamAsync more efficient?
    – Emil
    Jul 8, 2023 at 21:37
  • @Emil This is a good question, quite frankly I don't think there is significant difference. It seems the GetStreamAsyncCore adds a little overhead only. Jul 11, 2023 at 8:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.