const exportDevice = ()=>{
exportExcel().then(res=>{
const blob = new Blob([res],{ type: "application/vnd.ms-excel" }); // 把得到的结果用流对象转一下
var a = document.createElement("a"); //创建一个<a></a>标签
a.href = URL.createObjectURL(blob); // 将流文件写入a标签的href属性值
a.download = "设备信息.xlsx"; //设置文件名
a.style.display = "none"; // 障眼法藏起来a标签
document.body.appendChild(a); // 将a标签追加到文档对象中
a.click(); // 模拟点击了a标签,会触发a标签的href的读取,浏览器就会自动下载了
a.remove(); // 一次性的,用完就删除a标签
}).catch(err=>{
})
}
export const exportExcel = (params?: object): ResponseType => http.get(
'/station/exportExcel',
{params},
{responseType:'blob'} // 解决乱码问题
)
或者
var axiosOption={
method:type,
url: baseUrl + url,
data: data,
timeout: 0,
headers: {'Content-Type': head}
}
if(data.responseType=='blob'){
axiosOption.responseType= 'blob'
}
注意:导出乱码问题,在axios的responseType:‘blob’,以上的axios已二次封装
网友评论