二进制对象前端下载

二进制对象前端下载


    exportDetails(id) {
      downLservice({ orderSn: id }).then(res => {
        if (res.data.type === 'application/json') {
          var s = res.data
          var b = new Blob([s])
          var reader = new FileReader()
          const t = this
          reader.addEventListener('loadend', function() {
            t.$message({
              type: 'error',
              message: JSON.parse(reader.result).message
            })
          })
          reader.readAsText(b)
        } else {
          const blob = new Blob([res.data], {
            type:
              'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
          })
          const downloadElement = document.createElement('a')
          const href = window.URL.createObjectURL(blob)
          downloadElement.href = href
          downloadElement.download = '精装代购订单详情' + id + '.xlsx'
          document.body.appendChild(downloadElement)
          downloadElement.click()
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
        }
      }).catch(err => {
        console.log(err)
      })
    },
上一篇:在vue中实现axios异步下载(vue-cli,脚手架)


下一篇:vue 前后端下载