首页 > 解决方案 > 无法从 Angular 8 下载 excel 文件,服务器端代码是 .net core webapi 3.1

问题描述

抱歉重复的问题,但现有的解决方案都没有解决我的问题。

尝试从 Angular8 下载 excel 文件时,出现以下错误。

在此处输入图像描述

我使用.net core web api 3.1实现了返回文件的方法。下面是返回excel文件的方法。

 [Route("downloadClaims")]
    [HttpGet]
    public FileResult DownloadClaims(int userId)
    {
      var memory = new MemoryStream();
        using (var stream = new FileStream("E:/AddClaim.xlsx", FileMode.Open))
        {
            stream.CopyTo(memory);
        }
        memory.Position = 0;

        var mimeType = "application/vnd.ms-excel";

        return File(memory, mimeType, "AddClaim.xlsx");
    }

处理Angular8中的下载功能。请在下面找到我的角度服务代码。

 downLoadAllClaims():Observable<Blob>
         {
        let httpOption = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'responseType': 'blob' as 'json',
            'Access-Control-Expose-Headers':'Content-Disposition'
            , 'observe': 'response'
          })
        }
          return this.http.get<Blob>(baseURL+'/downloadClaims',httpOption);
       }

我的组件代码如下。

 downLoadClaim(_evnt):void{
this.claimService.downLoadAllClaims().subscribe(
  blob => {
    saveAs(blob,"AllClaims.xlsx")
  },
  error => {
    console.log("Something went wrong while downloading");
  });

}

文件没有下载。不明白我错过了什么?

标签: angular8asp.net-core-webapi

解决方案


推荐阅读