blob 导出新窗口请求头加token

const downloadOpera = throttle(function(url, filename) {
  let xhr = new XMLHttpRequest();
  xhr.open('GET', url,true);
  xhr.setRequestHeader("Authorization", 'Bearer ' + token.value);
  xhr.responseType = 'blob';
  xhr.onload = function(e) {
    //如果请求执行成功
    if (this.status == 200) {
      let blob = this.response;
      let a = document.createElement('a');
      let url = URL.createObjectURL(blob);
      a.href = url;
      if (filename) {
        a.download = filename;
      }
      a.click();
      window.URL.revokeObjectURL(url);
    }
  };
  xhr.send();
}, 3000, {
  leading: true,
  trailing: false
})
// 下载
export function downloadOpera (url, params?) {
  url = `${url}${params ? '?'.concat(qs.stringify(params)) : ''}`
  let xhr = new XMLHttpRequest();
  xhr.open('GET', url,true);
  xhr.setRequestHeader("Authorization", 'Bearer ' + store.token);
  xhr.responseType = 'blob';
  xhr.onload = function(e) {
    if (xhr.readyState === 4 && xhr.status === 200) {
      let res = xhr.getAllResponseHeaders()
      let result = res.split('\n')[1]
      console.log(res)
      console.log(result)
      let name = result.split('filename=')[1].replace(/^\s*/, '').replace(/\s*$/, '')
      let blob = xhr.response;
      let a = document.createElement('a');
      let url = URL.createObjectURL(blob);
      a.href = url;
      a.download = name
      a.click();
      window.URL.revokeObjectURL(url);
    }
  };
  xhr.send();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值