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

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

    Set response-header { responseType: 'blob'} in the request.

    To download the excel, use the below function.

    response - api response, fileName- excel name

    downloadExcel(response, fileName) {
        // Doing it this way allows you to name the file
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(res);
        link.download = fileName;
        link.click();
      }
    

提交回复
热议问题