vue下载文件
vue下载文件
废话不多说,直接上代码(url换成你的文件地址即可):
this.$axios.get(url, { responseType: 'blob', params: {}}).then((res) => {
let fileName = row.file_name;
if (!res.data) {
this.$message.warning(file + "下载失败!");
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([res.data]), fileName);
} else {
let url = window.URL.createObjectURL(new Blob([res.data]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放掉blob对象
}
}).catch((response) => {
this.$message.warning(file + "下载失败!");
});