1.需要后端提供一个接口
//方式一
// let url = 'xxxxxxxxxx/downLoadPlanExcel';
// window.location.href=url;
//方式二
// let url = 'xxxxxxxxx/downLoadPlanExcel';
// let div = document.createElement("div");
// div.innerHTML =
// '<iframe id="idFrame" name="idFrame" src="' +
// url +
// '" height = "0" width = "0" frameborder="0" scrolling="auto" style = "display:none;visibility:hidden" ></iframe>';
// document.body.appendChild(div);
//
// 方式三
handlePreview(fileInfo) {
let params = {}
let url = (CURRENT_URL + this.$CST.API_TASK_FILE_DOWNLOAD).replace('{fileId}', fileInfo.attachFileId)
let request = {
url,
data: params,
method: 'get',
headers: {
'Authorization': getCookie('token')
},
responseType: 'blob',
}
axios(request).then(res => {
if (res.status == 200) {
let blob = new Blob([res.data], {});
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileInfo.fileName)
} else {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileInfo.fileName;
link.click();
window.URL.revokeObjectURL(link.href)
}
}
}).catch((reason) => {
this.$notify({
title: '提示',
message: '失败' + reason,
duration: 0
});
})
},
参考:https://blog.csdn.net/MoXinXueWEB/article/details/126092614 前端实习下载的几种方法