Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly download excel file with Angular2

This is the code of my service which makes post request with response an xls file :

exportInternalOrder(body) {
        let user_token: string = this.sessionService.getToken();
        let headers = new Headers();
        headers.append('responseType', 'arraybuffer');
        headers.append('Authorization', 'Bearer ' + user_token);
        return this.http.post(this.config.exportInternalOrder, body,{
            headers: headers
        }).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' }));
    }

Which is supposed to handle response of excel file. This is the code invoking it:

let objToSend = this.makeObjToSend(false);
this.reportingService.exportExcel(objToSend)
    .subscribe(
        data => {
            this.exportData(data);
        },
        error => {
            this.errorFilterMsg.push({ severity: 'error', detail: 'Report exporting has failed!' });
        }
);

And this is the saving of the file (for some reason window.open does nothing):

exportData(data){       
        let blob = data;
        let a = document.createElement("a");
        a.href = URL.createObjectURL(blob);
        a.download = 'fileName.xls';
        document.body.appendChild(a);
        a.click();        
    }

But the file still saves as corrupted one. While using postman and curl it comes ok. Any help would be appreciated.

like image 338
Abhishek Subradeep Avatar asked Apr 07 '17 11:04

Abhishek Subradeep


People also ask

How do you make an Excel file downloadable?

Select File > Save As > Download a Copy. If Excel asks whether to open or save the workbook, select Save. Note: If you select Open instead of Save, the workbook will open in Protected View. Depending on your browser, you may not be asked this.

How do I download an Excel file in SAP?

a. From the blue menu bar, select “LIST” → “EXPORT” → “Local File”. b. From the blue menu bar, select “SYSTEM” → “LIST” → “SAVE” → “LOCAL FILE”.


2 Answers

responseType shouldn't be set in headers, it's part of RequestOptionsArgs object which is passed as second argument in post function and RequestOptionsArgs contains headers, responseType and others, you can read more about it here. So, your code should look like this:

import { ResponseContentType } from '@angular/http';

exportInternalOrder(body) {
    let user_token: string = this.sessionService.getToken();
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + user_token);
    return this.http.post(this.config.exportInternalOrder, body,{
        headers: headers,
        responseType: ResponseContentType.Blob
    }).map(res => new Blob([res._body],{ type: 'application/vnd.ms-excel' }));
}
like image 113
Stefan Svrkota Avatar answered Oct 20 '22 08:10

Stefan Svrkota


I am writing this..it will be helpful for others who are looking for Angular 2 solution for file download functionality. Below piece of code works for me.

import { ResponseContentType } from '@angular/http';

exportInternalOrder(body) {
    let user_token: string = this.sessionService.getToken();
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + user_token);
    return this.http.post(this.config.exportInternalOrder, body,{
        headers: headers,
        responseType: ResponseContentType.Blob}).map(res => new Blob([res.blob()],{ type: 'application/vnd.ms-excel' }));
}
like image 31
Leni MadiReddy Avatar answered Oct 20 '22 08:10

Leni MadiReddy