function download (res) { //res是后台返回的数据
let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); //type为所需要下载的文件格式,以xlsx文件为例
let url = window.URL.createObjectURL(blob);
let fileName = "详情.xlsx";
if ("download" in document.createElement("a")) {
let a = document.createElement("a");
a.href = url;
a.download = fileName;
a.style.display = "none";
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(a.href);
document.body.removeChild(a);
} else {
navigator.msSaveBlob(blob, fileName);
}
}
不同格式的文件对应的type :
文件类型 | type |
---|---|
.doc | application/msword |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.xls | application/vnd.ms-excel |
.xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.txt | text/plain |