How to download an excel file in Angular 8 as an API response

后端 未结 6 2541
长发绾君心
长发绾君心 2021-02-16 00:27

I\'ve an API which returns an excel document as response. The request will be one simple json.

I\'ve searched google and found some code base to download the file and I

6条回答
  •  花落未央
    2021-02-16 00:29

    1. You need to set the response-header { responseType: 'blob'} in the request.

    2. You need to pass in the correct MIME-Type as you'are creating the blob-file. (f.e. application/octet-stream or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet).

    component.ts

    downloadFile(data: Response) {
      const blob = new Blob([data], { type: 'application/octet-stream' });
      const url= window.URL.createObjectURL(blob);
      window.open(url);
    }
    

提交回复
热议问题