vue 导出文件空白 导出EXCEL打不开

问题描述

请求后台接口导出文件,文件打开空白页面但是有页码和大小

解决办法:

1》请求接口是否设置了responseType

2》确认项目是否引入MOCK

原因分析:

分析是返回的数据流有丢失,所以在上面的代码段将返回的res的结果给打印出来。

发现返回的data是一串乱码,并不是blob格式的文件流。

通过这个情况,在网上搜了半天,才发现是Mockjs污染了responseType,默认会将responseType变为''

那么第一种解决办法已经有了,就是修改Mockjs,文件路径是node_modules>mockjs>dist>mock.js

// 原生 XHR

if (!this.match) {

this.custom.xhr.responseType = this.responseType //新加的解决该问题的代码

this.custom.xhr.send(data)

return

}

那么这样改后,重新编译,测试,发现问题解决,返回值如下图。

但是这样只适合个人开发,实际上协同开发,或者用Jenkins自动部署时,会从npm下载Mockjs的源码,还是会有问题,那么怎么在不改Mockjs源码的情况下,解决该问题呢?

解决方案:

那么唯一的解决办法就是重写mockjs相关的部分,具体代码参考下面的

// 修复 mockjs 相关 bug

Mock.XHR.prototype.send = (() => {

const _send = Mock.XHR.prototype.send

return function() {

if (!this.match) {

this.custom.xhr.responseType = this.responseType || ''

this.custom.xhr.timeout = this.timeout || 0

this.custom.xhr.withCredentials = this.withCredentials || false

this.custom.xhr.onabort = this.onabort || null

this.custom.xhr.onerror = this.onerror || null

this.custom.xhr.onload = this.onload || null

this.custom.xhr.onloadend = this.onloadend || null

this.custom.xhr.onloadstart = this.onloadstart || null

this.custom.xhr.onprogress = this.onprogress || null

this.custom.xhr.onreadystatechange = this.onreadystatechange || null

this.custom.xhr.ontimeout = this.ontimeout || null

}

return _send.apply(this, arguments)

}

})()

相关推荐
candyTong1 天前
一觉醒来,大模型就帮我排查完页面性能问题
前端·javascript·架构
魔术师Grace1 天前
我给 AI 做了场入职培训
前端·程序员
玩嵌入式的菜鸡1 天前
网页访问单片机设备---基于mqtt
前端·javascript·css
前端一小卒1 天前
我用 Claude Code 的 Superpowers 技能链写了个服务,部署前差点把服务器搞炸
前端·javascript·后端
滑雪的企鹅.1 天前
HTML头部元信息避坑指南大纲
前端·html
一拳不是超人1 天前
老婆天天吵吵要买塔罗牌,我直接用 AI 2 小时写了个在线塔罗牌
前端·ai编程
阿丰资源1 天前
SpringBoot+Vue实战:打造企业级在线文档管理系统
vue.js·spring boot·后端
excel1 天前
如何解决 Nuxt DevTools 中关于 unstorage 包的报错
前端
Rust研习社1 天前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
C澒1 天前
AI 生码 - API2Code:接口智能匹配与 API 自动化生码全链路设计
前端·低代码·ai编程