UNPKG

1.11 kBPlain TextView Raw
1import {Bean} from "./context/context";
2
3@Bean("downloader")
4export class Downloader {
5 download(fileName:string, content:string, mimeType:string) {
6 // for Excel, we need \ufeff at the start
7 // http://stackoverflow.com/questions/17879198/adding-utf-8-bom-to-string-blob
8
9 // Internet Explorer
10 if (window.navigator.msSaveOrOpenBlob) {
11 let blobObject = new Blob(["\ufeff", content], {
12 type: mimeType
13 });
14 window.navigator.msSaveOrOpenBlob(blobObject, fileName);
15 } else {
16 // Chrome
17 const element = document.createElement("a");
18 const blob = new Blob(["\ufeff", content], {type: "octet/stream"});
19 const url = window.URL.createObjectURL(blob);
20 element.setAttribute("href", url);
21 element.setAttribute("download", fileName);
22 element.style.display = "none";
23 document.body.appendChild(element);
24 element.click();
25 window.URL.revokeObjectURL(url);
26 document.body.removeChild(element);
27 }
28 }
29}
\No newline at end of file