I'm pretty new one to angular and I need to create blob xlsx file. I have tried many different ways, like changing content-type to different formats etc. but nothing helps. I get error when I try to load xlsx file through Excel. If I download directly through URL it works fine. I have no idea what's wrong.
t.prototype.downloadXlsx = function () {
var t = this, e = {
date_from: this.helpers.formatDate(this.filter.date_from),
date_to: this.helpers.formatDate(this.filter.date_to),
download: "fees"
};
this.http.get("team-accounts/" + this.route.snapshot.params.id, e).subscribe(function (n) {
var i = {type: 'application/octet-stream'},
r = t.account.team.name + "_summary_" + e.date_from + "_" + e.date_to + ".xlsx";
t.helpers.downloadXlsxFile(n._body, i, r);
})
t.prototype.downloadXlsxFile = function (t, e, n, i) {
var r = new Blob(t], e);
if (navigator.msSaveBlob) navigator.msSaveBlob(r, n); else {
var a = document.createElement("a");
if (void 0 !== a.download) {
var o = i || document.body, s = URL.createObjectURL(r);
a.setAttribute("href", s), a.setAttribute("download", n), a.style.visibility = "hidden", o.appendChild(a), a.click(), o.removeChild(a)
}
}
You can try like this,
t.prototype.downloadXlsx = function () {
var t = this, e = {
date_from: this.helpers.formatDate(this.filter.date_from),
date_to: this.helpers.formatDate(this.filter.date_to),
download: "fees"
};
this.http.get("team-accounts/" + this.route.snapshot.params.id, e).subscribe( n => {
var url = t.account.team.name + "_summary_" + e.date_from + "_" + e.date_to + ".xlsx";
// file download section
const a = document.createElement('a');
document.body.appendChild(a);
const blob = new Blob([atob(n['_body'])], { type: 'octet/stream' });
// if octet/stream is not working then try this one application/ms-excel
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "filename.xlsx"; // you need to write the extension of file here
a.click();
window.URL.revokeObjectURL(url);
})
I hope it helps you out.