// 附件下载
AttachmentDownload(item, type) {
let data = ""
if (type == 'topic') {
data = { 'attachname': item.attachname, 'attachpath': '/data/PointTransfer/uploadFiles/topic/' + item.attachpath }
} else if (type == 'reply') {
data = { 'attachname': item.attachname, 'attachpath': '/data/PointTransfer/uploadFiles/reply/' + item.attachpath }
}
// const url = GlobalUrl.wjcs + '/wjcs/PointTransfer/upload/downloadFile.do'
var params = 'uploadJson=' + JSON.stringify(data)
this.$axios({
// url: GlobalUrl.wjcs + `/wjcs/PointTransfer/upload/downloadFile.do `,
url: `/api3/PointTransfer/upload/downloadFile.do `,
method: "post",
data: params,
responseType: "blob", // 重点在于配置responseType: 'blob'
headers: {
token: localStorage.getItem("token"), //token换成从缓存获取
},
}).then((res) => {
const link = document.createElement("a"); // 创建元素
let blob = new Blob([res.data], { type: res.headers["content-disposition"] }
);
link.style.display = "none";
link.href = URL.createObjectURL(blob); // 创建下载的链接
//num++
link.setAttribute("download", item.attachname); // 给下载后的文件命名
document.body.appendChild(link);
link.click(); // 点击下载
document.body.removeChild(link); // 下载完成移除元素
window.URL.revokeObjectURL(link.href); // 释放掉blob对象
})
2种写法