方法一:
const downloadRes = async () => {
let response = await fetch(url);
let blob = await response.blob();
let objectUrl = window.URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = objectUrl;
a.download = res.success.fileName;
a.click()
a.remove();
}
downloadRes();
方法二:(会出现跨域问题)
fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
const a = document.createElement('a')
a.href = URL.createObjectURL(blob)
console.log(a.href)
a.download = res.success.fileName // 下载文件的名字
document.body.appendChild(a)
a.click()
})
方法三:不跳转页面,直接下载。最完美!!!
if (res.code === 0) {
let url = res.success.url
const a = document.createElement('a')
a.href = url
a.download = res.success.fileName // 下载后文件名
a.style.display = 'none'
document.body.appendChild(a)
a.click() // 点击下载
document.body.removeChild(a) // 下载完成移除元素
}
方法四:最简单粗暴,一行代码搞定
window.location.href = res.success.url
标签:VUE,url,res,href,let,blob,document,下载
From: https://www.cnblogs.com/java-six/p/16855602.html