c# - 我可以不通过内存直接从 HttpResponseMessage 流式传输到文件吗?

标签 c# stream asp.net-web-api2

我的程序使用 HttpClient 向 Web API 发送 GET 请求,并返回一个文件。

我现在使用此代码(简化)将文件存储到光盘:

public async Task<bool> DownloadFile()
{
    var client = new HttpClient();
    var uri = new Uri("http://somedomain.com/path");
    var response = await client.GetAsync(uri);

    if (response.IsSuccessStatusCode)
    {
        var fileName = response.Content.Headers.ContentDisposition.FileName;
        using (var fs = new FileStream(@"C:\test\" + fileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            await response.Content.CopyToAsync(fs);
            return true;
        }
    }

    return false;
}

现在,当这段代码运行时,进程会将所有文件加载到内存中。实际上,我更希望流从 HttpResponseMessage.Content 流式传输到 FileStream,这样只有一小部分保留在内存中。

我们计划在大文件(> 1GB)上使用它,那么有没有办法在不将所有文件都放在内存中的情况下实现这一点?

理想情况下,无需手动循环读取一部分到 byte[] 并将该部分写入文件流,直到写入所有内容?

最佳答案

这看起来像是设计使然 - 如果您查看 HttpClient.GetAsync() 的文档,您会看到它说:

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

您可以改为使用 HttpClient.GetStreamAsync(),它具体说明:

This method does not buffer the stream.

但是据我所知,您然后可以访问响应中的 header 。因为这可能是一个要求(因为你从标题中获取文件名),所以你可能想要使用 HttpWebRequest 而不是它允许你在不阅读的情况下获取响应详细信息(标题等)整个 react 进入内存。像这样的东西:

public async Task<bool> 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
}

请注意,如果请求返回不成功的响应代码,将抛出异常,因此您可能希望包装在 try..catch 中并返回 false与您的原始示例一样。

关于c# - 我可以不通过内存直接从 HttpResponseMessage 流式传输到文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39033927/

相关文章:

c# - 无法从 WPF 中的集合绑定(bind)模型的 DependcyProperty

c# - WPF 中 DataGrid 的双向绑定(bind)

Node.js 同时读写流到同一个文件

iis - 异步操作方法对 IIS 有什么影响?

c# - Web API 帖子正文属性 null

c# - 使用 p/Invoke 的 x64 版本的 .net 应用程序崩溃

C# - 检查 Windows10 上是否安装了 UWP 应用程序

c# - 将大文件流式传输到数据库 BLOB 字段

c++ - friend 没有得到私有(private)成员(member)

c# - .NET Web api 2 在 IIS 中的某个时间后停止工作