使用React和AspnetCore下载大文件以打开浏览器保存(标题)

我正在为使用ASP网络核心中的Web API和React中的前端下载大型文件而苦恼。

文件开始下载时,直到将文件下载到内存中,然后才可以保存文件,才显示对话框浏览器(“另存为文件”对话框)。

文件较大时(例如200MB),用户无法选择文件保存位置,然后再开始下载并在浏览器标签中查看下载进度。

这是与aspnet核心中的react前端和Web API一起使用的。 看了几个小时后,我找不到解决方案。 我无法使用链接下载文件,因为我需要使用令牌进行身份验证。

也许我的后端/前端缺少任何配置或设置?

任何建议都将不胜感激。

我的Web API的方法:

        [HttpGet("{fileName}")]
        [Authorize]
        public IactionResult GetFile(string fileName)
        {

            Stream stream = _fileManager.GetFileContent(fileName);
            // Response...
            ContentDisposition cd = new ContentDisposition("attachment")
            {
                FileName = fileName,// DispositionType = "attachment; filename=" + fileName + ";",Inline = false // false = prompt the user for downloading;  true = browser to try to show the file inline
            };

            Response.Headers.Add(HeaderNames.ContentDisposition,cd.ToString());

            return File(stream,MediaTypeNames.Application.Zip);
        }

使用axios的前端。

async function Download() {
  var authToken = await authService.Token();

  console.log("calling request",process.env.REact_APP_API_URL + "files/GetFile/iPro.zip");
  if (authToken) {
    BioAxios.get("/files/GetFile/file.zip",{
      responseType: "blob",headers: {
        Authorization: "Bearer ".concat(authToken.access_token),"Content-Type": "application/zip"
      }
    }).then(response => {
      // Log somewhat to show that the browser actually exposes the custom HTTP header
      console.log("Full Response",response);

      // Let the user save the file.
      FileSaver.saveAs(response.data,"file.zip");
    });
  }
}

ZXY199 回答:使用React和AspnetCore下载大文件以打开浏览器保存(标题)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152007.html

大家都在问