关于vue.js:vue-根据后台传回文件流导出excel

因为业务需要,须要用到导出excel性能,后盾传回的是文件流

因而前端须要做点儿解决能力导出
要害代码

this.$ajax.post('/ship/order/exportOrder',param,{ responseType: 'arraybuffer' }).then(res=>{
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob);
    } else {
        const aLink = document.createElement('a');
        const blob = new Blob([res.data],{type: "application/vnd.ms-excel"});
        // 创立一个以后文件的内存URL
        const _href = URL.createObjectURL(blob);
        aLink.style.display = 'none';
        aLink.href = _href;
        document.body.appendChild(aLink);
        aLink.setAttribute('download', '订单数据.xlsx');
        aLink.click();
        document.body.removeChild(aLink);
        // 手动开释创立的URL对象所占内存
        URL.revokeObjectURL(_href);
    }
})

下面代码的关键在于 { responseType: 'arraybuffer' } 这个指定类型,之前遗记增加这个参数,尽管最初也能导出一个excel文件,然而关上的时候却失去报错 “此文件损坏无奈加载”;
所以这个不能少

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理