vue利用流的方式下载Excel(附解决下载失败)

  1. 解决下载文件打不开 https://www.csdn.net/tags/MtTaMg4sNDcyMTgwLWJsb2cO0O0O.html

最主要是:导出文档,调用接口是设置 responseType: “blob” ,不能写在headers内,要单独设置。

Export(){
axios({
method: “get”,
url: xxxxxx,
responseType: “blob” , //必写
headers: {
‘token’: localStorage.getItem(‘token’)
}
}).then((res)=>{
//new blob对象,设置type为导出文件格式,这里以xls为列
let blob = new Blob([res.data],
{ type:‘application/vnd.ms-excel;charset=utf-8;’ }
);

    const link = document.createElement("a");  //创建a标签
    let fliaName = "xx.xls"
    link.style.display = "none";
    link.href = URL.createObjectURL(blob);
    link.download = fliaName;  //导出文件名
    document.body.appendChild(link);
    link.click();//执行下载
    URL.revokeObjectURL(link.href);  //释放url
    document.body.removeChild(link);  //释放标签
    this.$message.success('导出成功!');
  })
},
  1. 解决下载操作

https://blog.csdn.net/weixin_46381442/article/details/123779751

import axios from "axios"
import * as auth from '@/utils/auth.js'

let ajax = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 100000
});

ajax.interceptors.request.use(config => {
        config.headers = {
            Authorization: auth.getToken(),
            // OrgId: auth.getUser().orgId,
            // UserId: auth.getUser().id,
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })

let downloadFile = async (url, formData, options) => {
    await ajax.post(url, formData, {responseType: 'arraybuffer'}).then(resp => download(resp, options))
}

let getFile = async (url, options) => {
    await ajax.get(url, {responseType: 'blob'}).then(resp => download(resp, options))
}

let download = (resp, options) => {
    let blob = new Blob([resp.data], {type: options.fileType ? options.fileType : 'application/octet-binary'})
    //创建下载的链接
    let href = window.URL.createObjectURL(blob)
    downloadBlob(href, options.fileName)
}

let downloadBlob = (blobUrl, fileName, revokeObjectURL) => {
    let downloadElement = document.createElement('a')
    downloadElement.href = blobUrl
    //下载后文件名
    downloadElement.download = fileName
    document.body.appendChild(downloadElement)
    //点击下载
    downloadElement.click()
    //下载完成移除元素
    document.body.removeChild(downloadElement)
    if (revokeObjectURL == null || revokeObjectURL) {
        //释放掉blob对象
        window.URL.revokeObjectURL(blobUrl)
    }
}

let getDownloadFileUrl = async (url, fileType) => {
    let blob
    await ajax.get(url, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return window.URL.createObjectURL(blob);
}

let getDownloadFileUrlByPost = async (url, data, fileType) => {
    let blob
    await ajax.post(url, data, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return window.URL.createObjectURL(blob);
}

let getDownloadFileBlob = async (url, fileType) => {
    let blob
    await ajax.get(url, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return blob;
}

export default {
    ajax,
    downloadFile,
    getFile,
    getDownloadFileUrl,
    getDownloadFileUrlByPost,
    getDownloadFileBlob,
    downloadBlob
}


调用:
//先引用

import ajax from '../../utils/ajax.js'
//使用
ajax.downloadFile('URL',null,{下载的文件名称})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值