大型zip文件(以GB为单位)存储在API层中。当用户单击浏览器中的下载按钮时,请求将通过WEB层转到API层,作为回报,我们需要将较大的文件从API层流到WEB层,然后流回到客户端浏览器。
请告知我如何在不将文件写入Web应用程序的情况下将大文件从API应用程序流传输到WEB应用程序到客户端?
Web应用程序使用Rest Sharp库请求API应用程序,如果您可以建议使用Rest Sharp(替代性本机代码)的解决方案,那就太好了。这两个项目都在.NET core 2.2中
答案 0 :(得分:0)
您要寻找DownloadData
吗?
https://github.com/restsharp/RestSharp/blob/master/src/RestSharp/RestClient.Sync.cs#L23
以下内容直接来自docs中的示例:
var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/LargeFile.7z");
request.ResponseWriter = responseStream =>
{
using (responseStream)
{
responseStream.CopyTo(writer);
}
};
var response = client.DownloadData(request);
答案 1 :(得分:0)
使用HttpClient而不是RestSharp库找到了解决方案,用于直接将内容下载到浏览器
代码段如下
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
};