下载按钮js方法

创建一个a标签下载:

// 模板下载
    templateDown() {
      const a = document.createElement('a')
      const evt = document.createEvent('MouseEvents')
      a.download = '导入模板'
      a.href = '/importTemplate.xls'
      evt.initEvent('click', true, true)
      a.dispatchEvent(evt)
      window.URL.revokeObjectURL(a.href)
    }

直接调后台接口下载:

// 下载文件
    downloadClick(row) {
      window.location.href = process.env.VUE_APP_BASE_API + '/sysFileInfo/publicDownload?fileId=' + row.id
    }

下载blob:

api.fileDownload(this.fileList[0]).then(res => {
        const currentdate = this.fileList[0].fileOriginName
        const blob = new Blob([res.data], { type: res.data.type })
        const url = window.URL.createObjectURL(blob) // URL.createObjectURL(object)表示生成一个File对象或Blob对象
        const dom = document.createElement('a') // 设置一个隐藏的a标签,href为输出流,设置download
        dom.style.display = 'none'
        dom.href = url
        dom.setAttribute('download', currentdate) // 指示浏览器下载url,而不是导航到它;因此将提示用户将其保存为本地文件
        document.body.appendChild(dom)
        dom.click()
      })

 

posted @ 2022-05-30 16:33  Ning-  阅读(173)  评论(0编辑  收藏  举报