二进制对象前端下载

二进制对象前端下载


    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)
      })
    },
posted @ 2021-01-17 12:20  南华秋水  阅读(74)  评论(0编辑  收藏  举报