You are on page 1of 2

npm install file-saver --save

import * as FileSaver from 'file-saver';

this.httpClient.get(this.excelDownloadUrl).subscribe(fileData => {

//fileData will be a JSON String with a 'name' and 'file' key in which
the 'file' key contains two parts, the 2nd part is
the base64 encoded data, so we have to first decode it using 'atob()'
function in Javascript
// console.log(atob(fileData["file"].split("base64,")[1]))

const byteCharacters = atob(fileData["file"].split("base64,")[1]);

const byteNumbers = new Array(byteCharacters.length);

for (let i = 0; i < byteCharacters.length; i++) {


byteNumbers[i] = byteCharacters.charCodeAt(i);
}

const byteArray = new Uint8Array(byteNumbers);

const blob = new Blob([byteArray], { type: 'application/vnd.ms-excel' });

//const blob: any = new Blob([fileData], { type: 'application/vnd.ms-excel'


});

const file = new File([blob], 'report.xlsx',


{ type: 'application/vnd.ms-excel' });

// var opts = [{ sheeitd: "Sheet 1", header: true }]


// alasql("SELECT INTO XLSX ('report.xlsx',?) FROM ?", [opts, [fileData]]);

FileSaver.saveAs(file);

//let url = URL.createObjectURL(blob);


//window.open(url)
//console.log(file)
})

//if response is blob

this.httpClient.get(this.excelDownloadUrl,{responsetype: 'blob' as
'blob'}).subscribe(fileData => {

//fileData will be a blob

const blob: any = new Blob([fileData], { type: 'application/vnd.ms-


excel' });

const file = new File([blob], 'report.xlsx',


{ type: 'application/vnd.ms-excel' });
FileSaver.saveAs(file);

//let url = URL.createObjectURL(blob);


//window.open(url)
//console.log(file)
})

You might also like