在Antd-Pro下实现文件下载

43次阅读

共计 568 个字符,预计需要花费 2 分钟才能阅读完成。

后端采用二进制流返回文件而不是常见的返回网络 URL
在 antd-pro 环境下
发送 ajax 请求时需要配置为
config = {
headers : {
token : tokenHandler.getSessionByKey(‘token’),
},
responseType : ‘blob’,
};
获取到数据之后, 在对应 modal 中用异步实现下载
* saveFile({payload: {blob, fileName}}, {call}) {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
var link = document.createElement(‘a’);
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
// 此写法兼容可火狐浏览器
document.body.appendChild(link);
var evt = document.createEvent(“MouseEvents”);
evt.initEvent(“click”, false, false);
link.dispatchEvent(evt);
document.body.removeChild(link);
}
},

正文完
 0