前置条件
后端已经把二进制流文件通过接口传给前端
前端开发
在ajax或axios设置api的时候,设置 responseType:'blob',这个很重要
如返回数据为res
const fileName ="xxx.xlsx";
const url = window.URL.createObjectURL(new Blob([res]));
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
利用blob将二进制数据转化成url,在用a标签的hrft属性,click事件下载excel