纯js实现前端获取后端文档流并下载到本地

先获取后端返回的文档流,然后创建一个a标签。把文档流转换成链接形式赋值给a并点击,最后移除a标签并释放刚刚创建的URL对象。

 const res = await 请求接口();
 const a = document.createElement('a');
 const blob = new Blob([res]);
 const href = window.URL.createObjectURL(blob);
 a.href = href;
 a.download = `下载文件名`;
 document.body.appendChild(a);
 a.click();
 document.body.removeChild(a);
 window.URL.revokeObjectURL(href);

你可能感兴趣的:(前端,javascript)