c# - C# .net core WEB-API "proxy" file download
问题描述
I have an angular frontend (with a pdf viewer) and a C# .net core API.
The .net core API is downloading a file (from an internal API) and then serves this file to the angular pdf viewer (like a proxy). It works fine but the load time is quite long as the backend downloads the whole file and then serves it to the frontend. Is there a way to retrieve the file in the api and at the same time deliver those pieces to the frontend?
So read the file in chunks to the memory stream and at the same time serve it to the frontend?
Currenty implementation:
WebClient webClient = new WebClient();
webClient.Headers.Add("Authorization", "Basic xxxxx");
var file = webClient.DownloadData("http://xxxx/" + id + "/pdf");
Stream stream = new MemoryStream(file);
return new FileStreamResult(stream, "application/pdf");
解决方案
Your issue is that DownloadData doesn't continue until it's downloaded the whole file. Try this to stream the data through instead
WebClient webClient = new WebClient();
webClient.Headers.Add("Authorization", "Basic xxxxx");
var stream = webClient.OpenRead("http://xxxx/" + id + "/pdf");
return new FileStreamResult(stream, "application/pdf");
推荐阅读
- android - Android Studio Kotlin Complier Error node.sym must not be null
- javascript - Highcharts 柱形图:创建颜色组
- django - csfr 令牌和 ajax 调用的问题
- c# - 多个文件下载为 zip angular 7/C#
- android - 如何使用 ID 从 sqlite 中获取值?
- php - PHP脚本出错,MySQL error 2006: mysql server has gone away
- python - 在浮点数上拆分python中的字符串,如果没有找到浮点数,将其拆分为一个数字
- c - 使用 wolfSSL 时出现 get_peer_certificate 错误
- c# - 如何阻止 azure b2c 令牌过期?
- azure-api-management - API 管理重试策略