使用vue实现文件下载功能,出现文件无法下载的问题
使用vue实现文件下载功能,出现文件无法下载的问题
·

如上图所示:我的下载代码是像下面这样写的
downloadFile(url) {
url = '../assets/ffffffff_01_ceshi.docx'
const link = document.createElement('a')
link.href = url
console.log(link.href)
link.setAttribute('download', '')
// link.download = 'ceshi.docx'
document.body.appendChild(link)
link.click()
window.URL.revokeObjectURL(link.href)
document.body.removeChild(link)
},
我一直以为是我的路径错了,后来看到一个帖子才知道,将待下载文件放在“static”或“assets”文件夹下,会始终获取不到待下载文件,vue cli3应该将待下载文件放在静待资源文件夹”public“中,(vue cli2应该放在‘static’中),如果放错了就会出现上面的报错!
正确写法:
将url改成 url = '../public/ffffffff_01_ceshi.docx'
更多推荐



所有评论(0)