繁体   English   中英

如何在 Angular 中接收下载 excel 文件的 blob 响应

[英]How to receive blob response for downloading excel file in Angular

我想使用 angular 在 excel 文件中下载 model,这是服务的代码 ZC1C425268E68385D1AB507:

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(不支持的媒体类型)。 我知道要使用的正确语法吗? 谢谢

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

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)

它对我有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM