简体   繁体   English

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

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

i want to download a model in an excel file using angular, this is function the code of my service:我想使用 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" });

when i call it i get an error: 415 (Unsupported Media Type).当我调用它时,我得到一个错误:415(不支持的媒体类型)。 Any i idea of the correct syntax to use?我知道要使用的正确语法吗? Thanks谢谢

Thank you for your help, yes i want only download it.I found this solution:感谢您的帮助,是的,我只想下载它。我找到了这个解决方案:

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`));
  }

In API:在 API 中:

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

It worked for me它对我有用

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

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