vue实现下载功能,处理后端返回的二进制文件流

          const content = res.data;   //后端返回的数据
          let url = window.URL.createObjectURL(content)
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('id', 'downloadLink')
          var date =
          new Date().getFullYear() +
          "" +
          (new Date().getMonth() + 1) +
          "" +
          new Date().getDate();
          link.setAttribute('download', date+'.xls')
          document.body.appendChild(link)
          // 删除添加的a链接
          let objLink = document.getElementById('downloadLink')
          objLink.click();
          document.body.removeChild(objLink)
          // 释放内存
          window.URL.revokeOjbectURL(url)

 

你可能感兴趣的:(vue,vue.js)