Using vue to realize the file download function, there is a problem that the file cannot be downloaded

 As shown above: My download code is written like this

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)
},

I always thought that my path was wrong, but later I saw a post and found out that if you put the file to be downloaded in the "static" or "assets" folder, you will never get the file to be downloaded. Vue cli3 should put the file to be downloaded The file is placed in the waiting resource folder "public", (vue cli2 should be placed in 'static'), if you put it wrong, the above error will appear!

Correct spelling:

Change the url to url = '../public/ffffffff_01_ceshi.docx'

Guess you like

Origin blog.csdn.net/weixin_45898996/article/details/129705073