vue实现下载附件功能

两种方式下载:

第一种:直接a标签下载

<a class="item-btn download" :href="'/xxx/xxx/download?id=' + xxx.id" :download="xxx.name">下载附件</a>

第二种: 

<el-button class="item-btn download" @click="downloadEnclosure(fileUrl, fileName)">下载附件</el-button>
  // 下载附件
    async downloadEnclosure(url, name) {
      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 = name
      a.click()
      a.remove()
    },

 

注: a标签通过js创建的方式:

handleClick(item){
  const fileUrl = `/xxx/xxx/download?id=${item.id}`
   const link = document.createElement('a')
   link.className = 'download_btn'
   link.href = fileUrl
   link.setAttribute('download',item.name)
   link.click()
   link.remove()
}

 

posted @ 2023-12-08 14:07  IT小姐姐  阅读(290)  评论(0编辑  收藏  举报