无法从 POST 请求中打开/下载 .xlsm 文件

时间:2021-05-12 13:30:49

标签: angular base64 blob export-to-excel

我一直在从所有来源和堆栈溢出问题中搜索和尝试很多,以从我从 POST 请求中收到的数据下载 excel 文件 (.xlsm)。我一直在使用 POST 请求将所有列和行的对象发送到后端,并返回下面的数据作为响应。

data: "UEsDBBQACAgIALlBqFIAAAAAAAAAAAAAAAALAAAAX3JlbHMvL... filename: report.xlsm

当我尝试下载并打开此文件时,它总是崩溃。所以我尝试设置 requestOptions (建议堆栈溢出) 作为一个 blob 并再次尝试,但仍然无法打开它。

此外,我通过设置所有标头尝试了 XMLHttpRequest,但仍然无法执行任何操作。 所以我猜测浏览器错误地解释了文件并以错误的方式下载。

有人可以帮我解决这个问题吗?

到目前为止我尝试过的:

使用 httpRequest 试试:

    const httpOption: Object = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      }),
      responseType: 'blob'
    };

    this.service.sendRequest(searchParameters, httpOption)
        .subscribe((response => this.downloadFile(response)));
        
    private onExportSuccess(response): void {
        var blob = new Blob([response.data], {type: 'application/json'});
        var filename = response.fileName.split('.').slice(0, -1).join('.');
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      // IE workaround
      window.navigator.msSaveOrOpenBlob(blob, response.fileName);
    } else {
      // Other browsers:
      const URL = window.URL || window.webkitURL;
      const downloadUrl = URL.createObjectURL(blob);
      if (filename) {
        // use HTML5 a[download] attribute to specify filename
        const a = document.createElement('a');
        a.href = downloadUrl;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
      }
      setTimeout(function() {
        URL.revokeObjectURL(downloadUrl);
      }, 100);
    }
  }

使用 XMLHttpRequest 试试:

let xhr = new XMLHttpRequest();
xhr.open('POST', AppConfig.service.dp + '/export/excel');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-ANTI-CSRF', 'ON');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);

xhr.responseType = 'blob';
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    const disposition = xhr.getResponseHeader('Content-Disposition');
    const filename = disposition.slice(disposition.indexOf('=') + 1, disposition.length);
    const contenttype = xhr.getResponseHeader('Content-Type');
    const blob = new Blob([xhr.response], { type: contenttype });
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      // IE workaround
      window.navigator.msSaveOrOpenBlob(blob, filename);
    } else {
      // Other browsers:
      const URL = window.URL || window.webkitURL;
      const downloadUrl = URL.createObjectURL(blob);
      if (filename) {
        // use HTML5 a[download] attribute to specify filename
        const a = document.createElement('a');
        a.href = downloadUrl;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
      }
      setTimeout(function() {
        URL.revokeObjectURL(downloadUrl);
      }, 100);
    }
  }
};
xhr.send(new URLSearchParams(JSON.stringify(searchParameters)).toString());
return;

我已经尝试了大多数事情,例如更改内容类型,但每次文件都已损坏。 请有人指导我如何解决这个问题。

0 个答案:

没有答案
相关问题