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

4次阅读

共计 684 个字符,预计需要花费 2 分钟才能阅读完成。

因为业务需要, 须要用到导出 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 文件, 然而关上的时候却失去报错 “ 此文件损坏无奈加载 ”;
所以这个不能少

正文完
 0