項目中用到流文件下載的需求,之前使用的方法一直都沒問題,但是這次就是下載不下來,查了多種方法終於解決了,方式如下:
// 下載文件
downLoadFile(e) {
let id = e.target.dataset.id;
let name = e.target.dataset.name;
if(e.target.dataset.id) {
this.$https({
method: 'get',
url: '/wyxt_ubqts_oncloud/common/download',
params: {
'file_id': '526f260d-73c1-422e-b10e-c0a2fe91364d'
},
responseType: "blob"
}).then(res => {
// console.log(res)
// const fileName = res.headers["content-disposition"].split("=")[1];
const _res = res.data;
let blob = new Blob([_res], {type: 'application/pdf'});
let downloadElement = document.createElement("a");
let href = window.URL.createObjectURL(blob); //創建下載的鏈接
downloadElement.href = href;
// downloadElement.download = fileName; //下載后文件名
downloadElement.download = name; //下載后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //點擊下載
document.body.removeChild(downloadElement); //下載完成移除元素
window.URL.revokeObjectURL(href); //釋放掉blob對象
});
}
},
