vue导出excel文件,wps提示无法打开文件,【responseType: ‘blob‘ 】

标题

解决办法:注释掉main文件中mock引用

// import './mock';

原因: mock改变了XMLHttpRequest,

导致config中responseType: ‘blob’,没起作用,返回二进制PK,而不是Blob对象
在这里插入图片描述

在这里插入图片描述

附导出方法

“file-saver”: “^2.0.2”

import { saveAs } from 'file-saver';

export const exportFile = (
  res: Blob,
  fileName = '导出文件',
  fileType = 'xlsx'
) => {
  return new Promise((resolve, reject) => {
    const fr = new FileReader();
    fr.readAsText(res);
    fr.onload = (e: any) => {
      // 导出报错
      if (e.target.result.indexOf('code') !== -1) {
        reject(JSON.parse(e.target.result).message);
      } else {
        let type = '';
        switch (fileType) {
          case 'xlsx':
          case 'xls':
            type = 'application/octet-stream';
            break;
          case 'txt':
            type = 'text/plain;charset=utf-8';
            break;
          case 'zip':
            type = 'application/zip';
            break;
          default:
            type = 'application/octet-stream';
            break;
        }
        // 导出成功
        const downloadData = new Blob([res], { type });
        saveAs(downloadData, `${fileName}.${fileType}`);
        resolve(1);
      }
    };
  });
};
后边代码自定义
const handleIntentionModel = () => {
  intentionModel().then(res => {
    exportFile(res.data, '意图测试模板', 'xlsx')
      .then(() => {
        message.success('下载成功');
      })
      .catch(err => {
        message.error(err);
      });
  });
};
    
<IntentTest ref="intentTestRef" :download-fn="handleIntentionModel" @frensh="handleReset" />
    
const handleDownTemplate = () => {
  if (downLoading.value) return;
  downLoading.value = true;
  props.downloadFn().finally(() => {
    downLoading.value = false;
  });
};


export function intentionModel() {
  return request({
    url: INTENTION_MODEL,
    responseType: 'blob',
    method: 'post',
    catchAll: true
  });
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eadela

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值