跨多层流式传输大文件 [英] Streaming large file across multiple layers

查看:60
本文介绍了跨多层流式传输大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大型zip文件(以GB为单位)存储在API层中.当用户单击浏览器中的下载按钮时,请求将通过WEB层转到API层,作为回报,我们需要将较大的文件从API层流到WEB层,然后流回到客户端浏览器.

Large zip file (in gigabytes) is stored in API layer. When a user clicks download button in the browser the request goes through WEB tier to the API tier and in return we need to stream the large file from API tier to WEB tier back to the client browser.

请告知我如何在不将文件写入Web应用程序的情况下将大文件从API应用程序流传输到WEB应用程序到客户端?

Please advice how can I stream large file from API application to WEB application to client without writing the file in web application?

Web应用程序使用Rest Sharp库请求API应用程序,如果您可以建议使用Rest Sharp(替代性本机代码)的解决方案,那就太好了.这两个项目都在.NET core 2.2中

The Web application request API applications using rest sharp library, it would be great if you can advice a solution using rest sharp (alternatively native code). Both the projects are in .NET core 2.2

推荐答案

使用HttpClient而不是RestSharp库(直接将内容下载到浏览器)找到了解决方案

Found the solution by using HttpClient instead of RestSharp library for downloading the content directly to browser

代码段如下

HttpClient client = new HttpClient();
var fileName = Path.GetFileName(filePath);
var fileDowloadURL = $"API URL";

var stream = await client.GetStreamAsync(fileDowloadURL).ConfigureAwait(false);
// note that at this point stream only contains the header the body content is not read

return new FileStreamResult(stream, "application/octet-stream")
{
    FileDownloadName = fileName
};

这篇关于跨多层流式传输大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆