需求是这样的......
具体实现,前端拿到后端返回回来的数据,然后通过Blob实现下载,文件内容样式啥的都是后端写的
script代码:
这里的data就是后端返回回来的数据,此方法兼容IE
1 download(data) { 2 if (!data) { 3 return; 4 } 5 let blob = new Blob([data], { 6 type: 7 "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8" 8 }); 9 let url = window.URL.createObjectURL(blob); 10 let fileName = this.data.plan_no + "出团通知单.docx"; 11 if ("download" in document.createElement("a")) { 12 const a = document.createElement("a"); 13 a.href = url; 14 a.download = fileName; 15 a.style.display = "none"; 16 document.body.appendChild(a); 17 a.click(); 18 URL.revokeObjectURL(a.href); 19 document.body.removeChild(a); 20 } else { 21 navigator.msSaveBlob(blob, fileName); 22 } 23 }