[vue] axios导出后端excel文件流处理

get请求

export const exportData = (params) => axios({
  url: url,
  method: 'get',
  responseType: 'blob',
  autoRes: true,
  params,
}).then((res) => res);

或者post请求

export const exportData = (data) => axios({
  url: url,
  method: 'post',
  responseType: 'blob',
  autoRes: true,
  data,
}).then((res) => res);

vue

const params = {
  name: this.name,
  age: this.age,
};
exportData(params).then((res) => {
  const blob = new Blob([res], {
    type: 'application/vnd.ms-excel;',
  });
  const link = document.createElement('a');
  link.href = window.URL.createObjectURL(blob);
  const format = new Date().getTime();
  link.download = `${format}.xlsx`;
  document.body.appendChild(link);
  link.click();
  window.URL.revokeObjectURL(link.href);
  document.body.removeChild(link);
});

如果下载之后会遇到文件无法打开的情况

经过排查是 mock.js 都会影响返回的数据,需要修改大约 8362 行出的代码

if (!this.match) {
    // 设置为原始的 xhr 响应
    this.custom.xhr.responseType = this.responseType
    this.custom.xhr.send(data)
    return
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值