其他分享
首页 > 其他分享> > 下载Excel(通过Blob对象)

下载Excel(通过Blob对象)

作者:互联网

const exportExtension = (params) => {
  return http({
    method: 'get',
    url: '/vsts/ui/v1/channelExtension/export',
    params,
    responseType: 'blob'
  })
}

download(name, blob) {
  try {
    const urlObject = window.URL || window.webkitURL
    const link = document.createElement('a')
    link.href = urlObject.createObjectURL(blob)
    link.download = name
    link.click()
  } catch (err) {
    console.log(err)
  }
},

exportExtension({
  regionId: this.regionId,
  cascade: this.cascade,
  ...params,
}).then(res => {
  console.log(res)
  if (res && res.data) {
    const contentDisposition = res.headers['content-disposition']
    // Content-Disposition 响应头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地
    if (contentDisposition && contentDisposition.includes('attachment')) {
      let filename
      if (contentDisposition.includes('filename')) {
        filename = decodeURI(contentDisposition.split(';')[1].replace('filename=', '')) // decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
      } else {
        filename = new Date().getTime()
      }
      if (window.navigator.msSaveOrOpenBlob) {
        // 兼容IE浏览器
        window.navigator.msSaveOrOpenBlob(res.data, filename)
      } else {
        this.download(filename, new Blob([res.data], { type: '' }))
      }
    }
  }
})

标签:下载,const,res,Excel,filename,window,link,Blob,contentDisposition
来源: https://blog.csdn.net/qq_41985405/article/details/121632834