excelExportTemplateBtn: function (){
const url=‘下载方法路径’
this.getBlob(url).then(blob => {
this.saveAs(blob, 'test.xlsx')
})
}
getBlob: function (url, options = {}) {
return new Promise(resolve => {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response)
}
}
xhr.send()
})
},
saveAs: function(blob, filename) {
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement("a");
tempLink.style.display = "none";
tempLink.href = blobURL;
tempLink.setAttribute("download", filename);
if (typeof tempLink.download === "undefined") {
tempLink.setAttribute("target", "_blank");
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
标签:文件,tempLink,vue,const,url,xhr,blob,document,下载
From: https://www.cnblogs.com/zwgblogs/p/18566519