VUE 下载文件流 文件无法打开,缺失数据

项目场景:

项目场景:VUE工程,做了一个代码自动生成可以导出zip的功能。


问题描述

导出的zip文件打开提示“不可预料的压缩文件末端”,文件打不开。
在这里插入图片描述

	export function downLoadZip(str, filename) {
	  var url = baseUrl + str
	  axios({
	    method: 'get',
	    url: url,
	    responseType: 'blob',
	    headers: { 'Authorization': 'Bearer ' + getToken() }
	  }).then(res => {
	    resolveBlob(res, mimeMap.zip)
	  })
	}

原因分析:

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

  1. 发现返回的data是一串乱码,并不是blob格式的文件流。
    在这里插入图片描述
  2. 通过这个情况,在网上搜了半天,才发现是Mockjs污染了responseType,默认会将responseType变为’’
  3. 那么第一种解决办法已经有了,就是修改Mockjs,文件路径是node_modules>mockjs>dist>mock.js
  	// 原生 XHR
          if (!this.match) {
          this.custom.xhr.responseType = this.responseType //新加的解决该问题的代码
              this.custom.xhr.send(data)
              return
          }
  1. 那么这样改后,重新编译,测试,发现问题解决,返回值如下图。
    在这里插入图片描述
  2. 但是这样只适合个人开发,实际上协同开发,或者用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)
  }
})()

具体代码查看 mock文件完整代码

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下载Excel文件是指通过Vue前端框架实现在网页上实时下载Excel文件的功能。为了实现这一功能,可以通过以下步骤: 1. 在Vue组件中使用axios或者其他网络请求库,向后端发送请求获取Excel文件数据。 2. 后端接收到请求后,根据接口要求生成Excel文件,并将文件返回给前端。 3. 前端接收到Excel文件数据后,通过Blob对象和URL.createObjectURL方法创建可供下载的Excel文件链接。 4. 在Vue组件中使用a标签或者其他方式,将Excel文件链接绑定到下载按钮或者表格中。用户点击下载按钮即可下载Excel文件。 在编写相关代码时,需要注意文件的处理、请求的发送和响应的处理。具体示例代码如下:(以axios为例) ```javascript // Vue组件中发送请求 <template> <div> <button @click="downloadExcel">下载Excel</button> </div> </template> <script> import axios from 'axios'; export default { methods: { downloadExcel() { axios({ method: 'get', url: 'http://your-backend-api-url/export-excel', responseType: 'blob', // 告诉axios返回的数据是二进制 }) .then(response => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'excel.xlsx'); document.body.appendChild(link); link.click(); }) } } } </script> ``` 以上就是使用Vue下载Excel文件的基本方法。通过这种方式,用户可以在网页上快速方便地下载后端生成的Excel文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值