【问题标题】:How to receive blob response for downloading excel file in Angular如何接收blob响应以在Angular中下载excel文件
【发布时间】:2020-12-29 23:45:16
【问题描述】:

我想使用 Angular 在 excel 文件中下载模型,这是我的服务代码的功能:

getExcelReport(data: myModel[]): Observable<Blob> {
 let params = new HttpParams()
      .set('myParam', JSON.stringify(data));

return this.Http.get(this.url + '/MyController/GetExcelReport', { params: params, observe: "body", responseType: "blob" });

当我调用它时,我得到一个错误:415(不支持的媒体类型)。 我知道要使用的正确语法吗? 谢谢

【问题讨论】:

  • 听起来像是服务器端问题?
  • 这是我在服务器端控制器中的内容` [HttpGet] [Route("GetExcelReport")] public async Task GetExcelReport([FromBody] IList myModel)`
  • 收到文件后你真正想要什么..?您是否希望用户简单地下载它?如果是这样,一个 window.open 可能会容易得多。如果您需要更多(例如在请求中设置身份验证标头),也许这会有所帮助:stackoverflow.com/questions/58335807/…

标签: angular typescript


【解决方案1】:

感谢您的帮助,是的,我只想下载它。我找到了这个解决方案:

getExcelReport(data: myModel[]): void {
    const url: string = this.url  + '/MyController/GetExcelReport';
    this.Http.post(url, data, { responseType: 'blob' })
      .subscribe((response: Blob) => saveAs(response, `report.xlsx`));
  }

在 API 中:

 [HttpPost] [Route("GetExcelReport")] public async Task<IActionResult> GetExcelReport([FromBody] IList<myModelType> myModel)

它对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多