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)

}

})()

相关推荐
广州华水科技几秒前
北斗GNSS变形监测系统是什么?主要有哪几种应用?
前端
晴天165 分钟前
【跨桌面应用开发】Neutralinojs快速入门指南
前端·javascript·electron·node.js
爱学习的程序媛40 分钟前
【Web前端】深入解析JavaScript异步编程
开发语言·前端·javascript·ecmascript·web
梧桐16841 分钟前
马克沁机枪上阵(二):前线开辟—Claude Code 如何用一天打通前端
前端
是上好佳佳佳呀44 分钟前
【前端(一)】HTML 知识梳理:从结构到常用标签
前端·html
楚轩努力变强1 小时前
2026 年前端进阶:端侧大模型 + WebGPU,从零打造高性能 AI 原生前端应用
前端·typescript·大模型·react·webgpu·ai原生·高性能前端
放下华子我只抽RuiKe51 小时前
深度学习 - 01 - NLP自然语言处理基础
前端·人工智能·深度学习·神经网络·自然语言处理·矩阵·easyui
AI服务老曹1 小时前
掌握核心代码:基于 Spring Boot + Vue 的 AI 视频管理平台源码架构与二次开发实战(全开源/低代码/私有化)
vue.js·人工智能·spring boot
酉鬼女又兒1 小时前
零基础入门前端 第十三届蓝桥杯省赛 :水果拼盘 Flex一篇过(可用于备赛蓝桥杯Web应用开发)
前端·css·职场和发展·蓝桥杯·css3
wefly20171 小时前
m3u8live.cn:免安装 HLS 在线播放器,流媒体调试效率神器
开发语言·javascript·python·django·ecmascript·hls.js 原理·m3u8 解析