后端返回文件流 前端导出表格

316 阅读1分钟

``` 方法实现:

   formatXlsxFn(content) {

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

const fileName = `SubscriptionReport ${moment().format('YYYYMMDD')}.xlsx`;

if ('download' in document.createElement('a')) {

// 非IE下载

const elink = document.createElement('a');

elink.download = fileName;

elink.style.display = 'none';

elink.href = URL.createObjectURL(blob);

document.body.appendChild(elink);

elink.click();

URL.revokeObjectURL(elink.href); // 释放URL 对象

document.body.removeChild(elink);

} else {

// IE10+下载

navigator.msSaveBlob(blob, fileName);

}

}

`

重要的请求头要加上,否则会报到处的文件损坏

  return request({

url: 'xxxxxxx',

responseType: 'arraybuffer',

method: 'get'

});