txt/jpg使用方式:下载
downloadFile(obj) {
if (!obj.filePath) return
const downloadElement = document.createElement('a')
const url = obj.filePath
fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
downloadElement.href = URL.createObjectURL(blob)
downloadElement.download = ''
document.body.appendChild(downloadElement)
downloadElement.click()
})
},
word/xlsx下载
downloadFile(obj) {
if (!obj.filePath) return
const downloadElement = document.createElement('a')
downloadElement.href = obj.filePath
downloadElement.download = obj.fileName
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
},
亲测可以~
这个博客分享了两种文件下载的方法,一种适用于txt/jpg格式,另一种针对word/xlsx文件。通过创建HTML的a标签并设置href和download属性,实现了浏览器内点击下载的功能。亲测有效。

被折叠的 条评论
为什么被折叠?



