其他分享
首页 > 其他分享> > 二进制对象前端下载

二进制对象前端下载

作者:互联网

二进制对象前端下载


    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)
      })
    },

标签:const,二进制,res,前端,reader,href,downloadElement,message,下载
来源: https://www.cnblogs.com/nanhuaqiushui/p/14288616.html