downloading xlsx file in angular 2 with blob

谁说我不能喝 提交于 2019-12-03 12:55:02

I was having the same problem as yours - fixed it by adding responseType: ResponseContentType.Blob to my Get options. Check the code below:

public getReport(filters: ReportOptions) {
    return this.http.get(this.url, {
            headers: this.headers,
            params: filters,
            responseType: ResponseContentType.Blob
        })
        .toPromise()
        .then(response => this.saveAsBlob(response))
        .catch(error => this.handleError(error));
}

The saveAsBlob() is just a wrapper for the FileSaver.SaveAs():

private saveAsBlob(data: any) {
    const blob = new Blob([data._body],
        { type: 'application/vnd.ms-excel' });
    const file = new File([blob], 'report.xlsx',
        { type: 'application/vnd.ms-excel' });

    FileSaver.saveAs(file);
}

After nothing works. I changed my server to return the same byte array with the addition of:

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=deployment-definitions.xlsx");

At my client I deleted the download function and instead of the GET part i did:

window.open(this.restUrl, "_blank");

This is the only way I found it possible to save an xlsx file that is not corrupted.

If you have a answer about how to do it with blob please tell me :)

El mehdi AZROUR

you can use the below code :

var file = new Blob([data], { 
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
});
saveAs(file,"nameFile"+".xlsx");
deferred.resolve(data);

The following solution worked for me in Google Chrome Version 58.0.3029.110 (64-bit).

It's an article explaining how to download a pdf using a Blob: https://brmorris.blogspot.ie/2017/02/download-pdf-in-angular-2.html

It's the same principle for a xlsx file. Just follow the steps from the article and change two things:

  • Header, instead of {'Accept': 'application/pdf'}, use {'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'};
  • Blob, use the same type for the creation of your Blob, new Blob([fileBlob], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}).

The article is using File Saver but I didn't. I left a comment, in the article, with a basic explanation of what I used instead of File Saver.

Here is the solution that supports IE and chrome/safari browsers. Here 'response' object is response received from service. I have this working for archive. You can change the type as per the response received from service.

    let blob = new Blob([response], {type: 'application/zip'});
    let fileUrl = window.URL.createObjectURL(blob);
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileUrl.split(':')[1] + '.zip');
    } else {
        this.reportDownloadName = fileUrl;
        window.open(fileUrl);
    }

For me problem was, that my responseType was "text". Instead I needed to use "blob"; It works for me on Angular 7.2.15.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!