后端返回数据流, 前端导出下载xls文件
this.$http({
url: '/pod/vehicle/alarmEvent/exportGet',
method: 'get',
responseType: 'blob', // 表明返回服务器返回的数据类型
headers: {
'Content-Type': 'application/json'
},
data: {
//接口参数
}
}).then((res)=>{
//创建一个隐藏的a连接,
const link = document.createElement('a');
let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'});
link.style.display = 'none';
//设置连接
link.href = URL.createObjectURL(blob);
link.download = '自定义文件名';
document.body.appendChild(link);
//模拟点击事件
link.click();
}).catch((err)=>{
console.log(err);
})
responseType值 xhr.response 数据类型 说明
"" String字符串 默认值(在不设置responseType时)
"text" String字符串
"document" Document对象 希望返回 XML 格式数据时使用
"json" javascript对象 存在兼容性问题,IE10/IE11不支持
"blob" Blob对象 后台传文档流可以用这个进行转换
"arrayBuffer" ArrayBuffer对象