vue post 下载文件

downloadFile(file){
// let url = baseURL + API.test.download + "/" + file.path;
// const link = document.createElement('a')
// link.href = url;
// link.download = file.name; // 重命名文件
// link.target="_blank"
// link.click();


this.$fetch.post(API.test.download + "/" + file.path,{}, {responseType: 'blob'}).then(response => {
let res = response.data;
if (res!=null) {
let blob = new Blob([res], { type: 'application/octet-stream' });
var reader = new FileReader();
reader.readAsBinaryString(blob);
reader.onloadend = () => {
// this.serverLog = reader.result;
this.$message.success(file.name + " download success");
}
let url = window.URL.createObjectURL(blob);
const link = document.createElement('a'); // 创建a标签
link.href = url;
link.download = file.name; // 重命名文件
link.click();
URL.revokeObjectURL(url); // 释放内存
}

});
},
posted @ 2022-04-06 17:04  牛牛——  阅读(516)  评论(0)    收藏  举报