Vue项目中的文件导出

项目中涉及到文件导出,分xml和excel导出。不同的文件导出格式不同,需要根据文件类型判断导出格式。

exportAllData(val){
//全部导出
if(!val){
this.exportFile(this.exportAllType);
}
},
exportFile(exportType){
let url='';//接口地址
this.$axios.get(url,{responseType: 'arraybuffer'}).then(res => {
this.download(res.data,exportType);
},res => {
this.$Message.error('导出失败');
});
},
download (data,exportType) {
if (!data) {
return
}
let exportGs='';
if(exportType==='excel'){
exportGs='application/vnd.ms-excel';
}else if(exportType==='xml'){
exportGs='text/xml';
}
let url = window.URL.createObjectURL(new Blob([data],{type: exportGs}));
let link = document.createElement('a')
link.style.display = 'none'
link.href = url;
link.setAttribute('download', '文件');
document.body.appendChild(link)
link.click();
}

posted @ 2018-07-13 15:00  heroljy  阅读(1455)  评论(0编辑  收藏  举报