前端下载blob格式excel文件 - 基于vue-element-template修改request.js文件

很多vue2项目可能都是根据这个模版来进行开发,在下载文件时可能会遇到一些问题导致下载不了,或者报错,因为这个框架在封装axios时,在响应拦截器中判断必须要返回值的,但是有个问题出现了,后台会直接把数据放在response.data中返回,并没有什么res.code、res.message之类的东西,因此我们需要新增改变一下拦截器中的东西。

在响应拦截器里,原本的写法就是这样的,让我们拿到返回值,来进行res.code等判断

service.interceptors.response.use(response => {

const res = response.data

...



})

现在我们在这个定义之下新增修改以下代码。

注:关于filename是后台传过来的名字,会直接放在response.headers中,如果没有可以自己设置或者和后台说一下给放上。

const isBlob = res instanceof Blob;
  if (isBlob) {
    // console.log('blob', res);
    let blob = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }) // 文件类型
    let filename = window.decodeURI(response.headers['content-disposition'].split('=')[1])
    let url = window.URL.createObjectURL(blob); // 创建下载链接
    let aLink = document.createElement('a'); // 赋值给a标签的href属性
    aLink.style.display = 'none';
    aLink.href = url;
    aLink.setAttribute('download', filename);
    document.body.appendChild(aLink); // 将a标签挂载上去
    aLink.click(); // a标签click事件
    document.body.removeChild(aLink); // 移除a标签
    window.URL.revokeObjectURL(url); // 销毁下载链接
  } else {
    if (res.code !== 20000 && res.code !== 200) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  }

 这样就能把blob流文件进行下载了。

在api中的请求方法需要添加responseType类型

...


exportRecord(data) {
    return request({
      url: `/service/reportrecord/exportReportRecord/?${data}`,
      method: 'get',
      responseType: 'blob', //要添加这个
    })
  },


...

 在methods里的方法(关于qs是啥可以百度一下,其实就是规范一下get请求格式,挺好用的)

async exportReportData() {
      let data = this.$qs.stringify({

       ...

      })
      await this.$http.reportingRecords.exportRecord(data)
    },

 以下是完整的request.js响应拦截器内容

// 响应拦截器
service.interceptors.response.use(response => {
  // 获取服务器响应的数据
  const res = response.data
  const isBlob = res instanceof Blob;
  if (isBlob) {
    // console.log('blob', res);
    let blob = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }) // 文件类型
    let filename = window.decodeURI(response.headers['content-disposition'].split('=')[1])
    let url = window.URL.createObjectURL(blob); // 创建下载链接
    let aLink = document.createElement('a'); // 赋值给a标签的href属性
    aLink.style.display = 'none';
    aLink.href = url;
    aLink.setAttribute('download', filename);
    document.body.appendChild(aLink); // 将a标签挂载上去
    aLink.click(); // a标签click事件
    document.body.removeChild(aLink); // 移除a标签
    window.URL.revokeObjectURL(url); // 销毁下载链接
  } else {
    if (res.code !== 20000 && res.code !== 200) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  }
}, error => {
  console.log('err' + error) // for debug
  Message({
    message: error.message,
    type: 'error',
    duration: 5 * 1000
  })
  return Promise.reject(error)
}
)

 若是有更好的方法,请多多指教~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wps-view-vue是一个基于Vue框架开发的文件在线预览组件,它可以帮助我们实现对blob格式文件的在线预览。下面我将详细介绍如何使用wps-view-vue来预览blob格式文件。 首先,我们需要在项目中引入wps-view-vue组件,可以通过npm或者其他方式进行安装。在Vue的组件中,我们可以使用import语句导入wps-view-vue组件。 ``` import WpsView from 'wps-view-vue' ``` 接下来,我们在Vue组件中使用wps-view-vue组件,并传入blob格式文件数据作为参数。 ``` <template> <div> <wps-view :blob="fileBlob" /> </div> </template> <script> import WpsView from 'wps-view-vue' export default { components: { WpsView }, data() { return { fileBlob: null } }, mounted() { // 获取blob格式文件数据,可以是通过接口请求获取到的 // 这里假设我们通过axios发送请求获取到了blob格式文件数据 axios.get('http://example.com/api/file', { responseType: 'blob' }) .then(response => { this.fileBlob = response.data }) .catch(error => { console.error(error) }) } } </script> ``` 以上代码中,我们将获取到的blob格式文件数据赋值给fileBlob变量,并作为参数传递给wps-view组件。 通过以上步骤,我们就可以在Vue项目中使用wps-view-vue组件实现对blob格式文件的在线预览了。wps-view-vue组件会根据文件类型选择合适的预览方式,确保用户可以在浏览器中直接查看和操作blob格式文件。这样可以提升用户的使用体验,并且方便用户对文件进行相关操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值