前端如何下载文件

if (res.status == 200) {
const content = res.data;
const blob = new Blob([content]);
const fileName = "合同约定模板.xls";
if ("download" in document.createElement("a")) {
// 非IE下载
const elink = document.createElement("a");
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
}

 

posted @ 2018-11-16 18:20  Hekiss  阅读(347)  评论(0编辑  收藏  举报