vue achieve download files through the Blob

Demand is so ......

The specific implementation, the front end to get the return back to the back-end data, and then achieve download Blob, what style file contents are written back end

script Code:

data here is returned back to the rear end of the data, this method is compatible 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     }

 

Guess you like

Origin www.cnblogs.com/jun-qi/p/10931432.html
Recommended