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

后端 未结 6 2515
长发绾君心
长发绾君心 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:46

    You need to Add { responseType : 'application/octet-stream'} in your get request as No need to call post method

    { responseType : 'application/octet-stream'}
    

    And in .ts file

    DownLoadExcel(){
        this.MprService.downLoadRFQInfoExcel(this.revisionId).subscribe(data =>{this.downloadFile(data)});
    }
        
    downloadFile(data: Blob) { 
        const contentType = 'application/vnd.openxmlformats-ficedocument.spreadsheetml.sheet'; 
        const blob = new Blob([data], { type: contentType });
        const url = window.URL.createObjectURL(blob);
        window.open(url);
    }
    

提交回复
热议问题