a标签调用接口下载文件

const a = document.createElement('a')
        document.body.appendChild(a);
        a.setAttribute('download', '')
        a.setAttribute('href', this.$baseURL + '/warehouse/export')
        a.setAttribute('id', 'downloadBtn')
        document.body.removeChild(document.getElementById('downloadBtn'))
        a.click();
axios({
          method: 'get',
          url: this.$baseURL + '/warehouse/export',
          responseType: 'blob'
        }).then(res => {
          console.log(res)
          var blob = res.data//blob对象
          var reader = new FileReader();
          reader.readAsDataURL(blob)
          reader.onload = function (e) {
            const a = document.createElement('a')
            a.download = '仓库.xlsx'
            a.href = e.target.result
            a.click()
          }

在这里插入图片描述

axios({
         method: 'get',
         url: this.$baseURL + '/warehouse/export',
         responseType: 'blob'
       }).then(res => {
         const content = res.data
         const blob = new Blob([content])
         const fileName = '下载的文件.xlsx'
         if ('download' in document.createElement('a')) {//非ie下载
           const elink = document.createElement('a')
           elink.download = fileName
           elink.style.display = 'none'
           elink.href = URL.createObjectURL(blob)
           document.body.appendChild(elink)
           elink.click()
           URL.revokeObjectURL(elink.href)
           document.body.removeChild(elink)
         } else {//ie10+下载
           navigator.msSaveBlob(blob, fileName)
         }
       })
发布了122 篇原创文章 · 获赞 5 · 访问量 4845

猜你喜欢

转载自blog.csdn.net/weixin_41254345/article/details/103808537