文件流导出
1.方法
// 批量导出
handleMutilExport = () => {
const req = new XMLHttpRequest();
req.open('POST', url(接口), true);
req.responseType = 'blob';
req.setRequestHeader('Content-Type', 'application/json');
req.onload = () => {
const data = req.response;
const a = document.createElement('a');
const blob = new Blob([data],{type:"application/octet-stream"});
const blobUrl = window.URL.createObjectURL(blob);
this.download(blobUrl) ;
};
req.send('');
};
download = (blobUrl) => {
const a = document.createElement('a');
a.style.display = 'none';
a.download = 'xxx.xls'; //文件名
a.href = blobUrl;
a.click();
// document.body.removeChild(a);
}
2.HTML
<Button type="primary" onClick={() => this.handleMutilExport()}>
批量导出
</Button>