Can I directly stream from HttpResponseMessage to file without going through memory?

前端 未结 2 1911
夕颜
夕颜 2021-01-11 18:28

My program uses HttpClient to send a GET request to a Web API, and this returns a file.

I now use this code (simplified) to store the file to disc:

p         


        
2条回答
  •  长情又很酷
    2021-01-11 18:54

    It looks like this is by-design - if you check the documentation for HttpClient.GetAsync() you'll see it says:

    The returned task object will complete after the whole response (including content) is read

    You can instead use HttpClient.GetStreamAsync() which specifically states:

    This method does not buffer the stream.

    However you don't then get access to the headers in the response as far as I can see. Since that's presumably a requirement (as you're getting the file name from the headers), then you may want to use HttpWebRequest instead which allows you you to get the response details (headers etc.) without reading the whole response into memory. Something like:

    public async Task DownloadFile()
    {
        var uri = new Uri("http://somedomain.com/path");
        var request = WebRequest.CreateHttp(uri);
        var response = await request.GetResponseAsync();
    
        ContentDispositionHeaderValue contentDisposition;
        var fileName = ContentDispositionHeaderValue.TryParse(response.Headers["Content-Disposition"], out contentDisposition)
            ? contentDisposition.FileName
            : "noname.dat";
        using (var fs = new FileStream(@"C:\test\" + fileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            await response.GetResponseStream().CopyToAsync(fs);
        }
    
        return true
    }
    

    Note that if the request returns an unsuccessful response code an exception will be thrown, so you may wish to wrap in a try..catch and return false in this case as in your original example.

提交回复
热议问题