axios(post方法) + vue 方法导出excel文件(已解决导出乱码问题)

1 返回的是二进制流文件
在这里插入图片描述
2 实现方法

fetch({
        url: '/statistics/preDetailStatisticsExcel',
        method: 'post',
        data: this.forms,  // 参数
        responseType: 'blob'  // 加上这句处理导出乱码问题
      }).then(res => {
        console.log(res)
        let blob = new Blob([res.data], {
          type: 'application/vnd.ms-excel'
        })
        let objectUrl = URL.createObjectURL(blob)
        let a = document.createElement('a')
        a.href = objectUrl
        a.download = formatDate(new Date()) // 文件名 ,
        // a.click();
        // 下面这个写法兼容火狐
        a.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}))
        window.URL.revokeObjectURL(blob)
      })
发布了70 篇原创文章 · 获赞 67 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_37896578/article/details/101365396
今日推荐