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)

}

})()

相关推荐
paopaokaka_luck10 分钟前
社区服务平台(加权协同过滤推荐、Jieba中文词频分析、WebSocket实时聊天、求助处理闭环、 活动积分闭环、ECharts多维分析)
java·前端·spring boot·mysql·echarts·mybatis
墨林陌22 分钟前
翻完 DeepSeek 的 GitHub 仓库,我发现 V5 的发布路已经修好了
前端
Antruly37 分钟前
uvscope:Web 调试界的瑞士军刀
前端
计算机魔术师1 小时前
Anthropic S-1 披露:半年前发布的模型,已贡献不了多少收入了
前端
qq_340040881 小时前
idea2021版,vue项目的package.json不展示scripts的绿色箭头
javascript·vue.js·json
qq_364682151 小时前
录音APP怎么切换语言?多语言版本如何使用
java·服务器·前端
2601_968900771 小时前
玻璃深加工的工艺窗口:4 道工序的温度与时间参数边界
android·java·javascript
志尊宝1 小时前
AI原生前端开发(037):AI辅助的Vue 3开发最佳实践
前端·vue.js·ai-native
中科三方1 小时前
AI时代新攻击面:悬空DNS记录正在成为黑客的低成本入侵入口
服务器·前端·人工智能
程序员大阳1 小时前
副队长JavaScript教程(53)-DOM操作元素的属性
javascript·dom·属性·setattribute·getattribuite