获取文件流
sid只是我这个项目获取文件流的一个参数,大家可以根据自己实际项目传参
getFile() {
queryFileData({
sid: this.sid
}).then(res => {
this.downloadUrl(res.data, '渠道创意短链接信息');
}).catch(err => {
console.log(err);
});
},
下载文件方法
// data: 文件流 name:文件名
downloadUrl(data, name) {
const blob = new Blob([data], {
type: "application/vnd.ms-excel"
});
// 兼容 IE
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
const fileName = name + ".xlsx";
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
// 非 IE 浏览器
const fileName = name + ".xlsx";
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);
document.body.removeChild(elink);
}
}
以上代码便可以轻松的下载excel文件啦,有需要的小伙伴自取哈~~