2021-01-27 在VUE中导出xls文件乱码的问题

在VUE中导出xls文件乱码的问题

(纯个人使用中记录,可能有很多不清楚的)

1、后台接口:这就不说了,毕竟这个接口也不是我开发的

2、前台接口的js文件,重点就是最后面的config参数:{ responseType: 'arraybuffer' },我的问题就出在这个上面,一开始没有加就导致乱码

  // 导出有效数据
exportValid (params) {
  return Vue.axios.post('...url...', params, { responseType: 'arraybuffer' })
},

3、具体的接口调用,使用blob,倒是一般的使用方式

const that = this
TestDataApi.importValidTemplate().then((result) => {
  const blob = new Blob([result], { type: 'application/vnd.ms-excel;charset=UTF-8' })
  window.console.log(blob)
  const downloadElement = document.createElement('a')
  const href = window.URL.createObjectURL(blob)
  window.console.log(href)
  downloadElement.href = href
  downloadElement.download = 'ColumnImportTemplate.xls'
  document.body.appendChild(downloadElement)
  window.console.log(downloadElement)
  downloadElement.click()
  document.body.removeChild(downloadElement)
  window.URL.revokeObjectURL(href)
}, (err) => {
  that.handleError(err)
})
  .catch((error) => {
    that.$message.error(error.toString())
  })

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值