Скачать BLOB-файл xlsx - PullRequest
       29

Скачать BLOB-файл xlsx

0 голосов
/ 26 апреля 2019

Я довольно новичок в angular, и мне нужно создать BLOB-файл xlsx.Я пробовал много разных способов, таких как изменение типа контента на разные форматы и т. Д., Но ничего не помогает.Я получаю сообщение об ошибке при попытке загрузить файл xlsx через Excel.Если я загружаю напрямую через URL, он работает нормально.Понятия не имею, что не так.

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)
                }
            }

1 Ответ

1 голос
/ 26 апреля 2019

Вы можете попробовать вот так,


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);
    })

Надеюсь, это поможет вам.

...