|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * FileSaver.js |
| 4 | + * A saveAs() FileSaver implementation. |
| 5 | + * |
| 6 | + * By Eli Grey, http://eligrey.com |
| 7 | + * |
| 8 | + * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT) |
| 9 | + * source : http://purl.eligrey.com/github/FileSaver.js |
| 10 | + */ |
| 11 | + |
| 12 | +import { globalObject as _global } from "./globalObject.js"; |
| 13 | +import { console } from "./console.js"; |
| 14 | + |
| 15 | +interface SaveAsOptions { |
| 16 | + autoBom?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +function bom(blob: Blob, opts?: SaveAsOptions | boolean): Blob { |
| 20 | + let options: SaveAsOptions; |
| 21 | + if (typeof opts === "undefined") { |
| 22 | + options = { autoBom: false }; |
| 23 | + } else if (typeof opts !== "object") { |
| 24 | + console.warn("Deprecated: Expected third argument to be a object"); |
| 25 | + options = { autoBom: !opts }; |
| 26 | + } else { |
| 27 | + options = opts; |
| 28 | + } |
| 29 | + |
| 30 | + // prepend BOM for UTF-8 XML and text/* types (including HTML) |
| 31 | + // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF |
| 32 | + if ( |
| 33 | + options.autoBom && |
| 34 | + /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test( |
| 35 | + blob.type |
| 36 | + ) |
| 37 | + ) { |
| 38 | + return new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type }); |
| 39 | + } |
| 40 | + return blob; |
| 41 | +} |
| 42 | + |
| 43 | +function download(url: string, name: string, opts?: SaveAsOptions): void { |
| 44 | + const xhr = new XMLHttpRequest(); |
| 45 | + xhr.open("GET", url); |
| 46 | + xhr.responseType = "blob"; |
| 47 | + xhr.onload = function() { |
| 48 | + saveAs(xhr.response, name, opts); |
| 49 | + }; |
| 50 | + xhr.onerror = function() { |
| 51 | + console.error("could not download file"); |
| 52 | + }; |
| 53 | + xhr.send(); |
| 54 | +} |
| 55 | + |
| 56 | +function corsEnabled(url: string): boolean { |
| 57 | + const xhr = new XMLHttpRequest(); |
| 58 | + // use sync to avoid popup blocker |
| 59 | + xhr.open("HEAD", url, false); |
| 60 | + try { |
| 61 | + xhr.send(); |
| 62 | + } catch (e) {} |
| 63 | + return xhr.status >= 200 && xhr.status <= 299; |
| 64 | +} |
| 65 | + |
| 66 | +// `a.click()` doesn't work for all browsers (#465) |
| 67 | +function click(node: HTMLAnchorElement): void { |
| 68 | + try { |
| 69 | + node.dispatchEvent(new MouseEvent("click")); |
| 70 | + } catch (e) { |
| 71 | + const evt = document.createEvent("MouseEvents"); |
| 72 | + evt.initMouseEvent( |
| 73 | + "click", |
| 74 | + true, |
| 75 | + true, |
| 76 | + window, |
| 77 | + 0, |
| 78 | + 0, |
| 79 | + 0, |
| 80 | + 80, |
| 81 | + 20, |
| 82 | + false, |
| 83 | + false, |
| 84 | + false, |
| 85 | + false, |
| 86 | + 0, |
| 87 | + null |
| 88 | + ); |
| 89 | + node.dispatchEvent(evt); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +type SaveAsFunction = (blob: Blob | string, name?: string, opts?: SaveAsOptions, popup?: Window | null) => void; |
| 94 | + |
| 95 | +const saveAs: SaveAsFunction = |
| 96 | + _global.saveAs || |
| 97 | + // probably in some web worker |
| 98 | + (typeof window !== "object" || window !== _global |
| 99 | + ? function saveAs(): void { |
| 100 | + /* noop */ |
| 101 | + } |
| 102 | + : // Use download attribute first if possible (#193 Lumia mobile) unless this is a native app |
| 103 | + typeof HTMLAnchorElement !== "undefined" && |
| 104 | + "download" in HTMLAnchorElement.prototype |
| 105 | + ? function saveAs(blob: Blob | string, name?: string, opts?: SaveAsOptions): void { |
| 106 | + const URL = _global.URL || _global.webkitURL; |
| 107 | + const a = document.createElement("a"); |
| 108 | + name = name || (blob as any).name || "download"; |
| 109 | + |
| 110 | + a.download = name; |
| 111 | + a.rel = "noopener"; // tabnabbing |
| 112 | + |
| 113 | + // TODO: detect chrome extensions & packaged apps |
| 114 | + // a.target = '_blank' |
| 115 | + |
| 116 | + if (typeof blob === "string") { |
| 117 | + // Support regular links |
| 118 | + a.href = blob; |
| 119 | + if (a.origin !== location.origin) { |
| 120 | + corsEnabled(a.href) |
| 121 | + ? download(blob, name, opts) |
| 122 | + : click(a as any); |
| 123 | + } else { |
| 124 | + click(a); |
| 125 | + } |
| 126 | + } else { |
| 127 | + // Support blobs |
| 128 | + a.href = URL.createObjectURL(blob); |
| 129 | + setTimeout(function() { |
| 130 | + URL.revokeObjectURL(a.href); |
| 131 | + }, 4e4); // 40s |
| 132 | + setTimeout(function() { |
| 133 | + click(a); |
| 134 | + }, 0); |
| 135 | + } |
| 136 | + } |
| 137 | + : // Use msSaveOrOpenBlob as a second approach |
| 138 | + "msSaveOrOpenBlob" in navigator |
| 139 | + ? function saveAs(blob: Blob | string, name?: string, opts?: SaveAsOptions): void { |
| 140 | + name = name || (blob as any).name || "download"; |
| 141 | + |
| 142 | + if (typeof blob === "string") { |
| 143 | + if (corsEnabled(blob)) { |
| 144 | + download(blob, name, opts); |
| 145 | + } else { |
| 146 | + const a = document.createElement("a"); |
| 147 | + a.href = blob; |
| 148 | + a.target = "_blank"; |
| 149 | + setTimeout(function() { |
| 150 | + click(a); |
| 151 | + }); |
| 152 | + } |
| 153 | + } else { |
| 154 | + (navigator as any).msSaveOrOpenBlob(bom(blob, opts), name); |
| 155 | + } |
| 156 | + } |
| 157 | + : // Fallback to using FileReader and a popup |
| 158 | + function saveAs(blob: Blob | string, name?: string, opts?: SaveAsOptions, popup?: Window | null): void { |
| 159 | + // Open a popup immediately do go around popup blocker |
| 160 | + // Mostly only available on user interaction and the fileReader is async so... |
| 161 | + popup = popup || open("", "_blank"); |
| 162 | + if (popup) { |
| 163 | + popup.document.title = popup.document.body.innerText = |
| 164 | + "downloading..."; |
| 165 | + } |
| 166 | + |
| 167 | + if (typeof blob === "string") return download(blob, name, opts); |
| 168 | + |
| 169 | + const force = blob.type === "application/octet-stream"; |
| 170 | + const isSafari = |
| 171 | + /constructor/i.test((_global as any).HTMLElement) || (_global as any).safari; |
| 172 | + const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent); |
| 173 | + |
| 174 | + if ( |
| 175 | + (isChromeIOS || (force && isSafari)) && |
| 176 | + typeof FileReader === "object" |
| 177 | + ) { |
| 178 | + // Safari doesn't allow downloading of blob URLs |
| 179 | + const reader = new FileReader(); |
| 180 | + reader.onloadend = function() { |
| 181 | + let url = reader.result as string; |
| 182 | + url = isChromeIOS |
| 183 | + ? url |
| 184 | + : url.replace(/^data:[^;]*;/, "data:attachment/file;"); |
| 185 | + if (popup) popup.location.href = url; |
| 186 | + else (location as any) = url; |
| 187 | + popup = null; // reverse-tabnabbing #460 |
| 188 | + }; |
| 189 | + reader.readAsDataURL(blob); |
| 190 | + } else { |
| 191 | + const URL = _global.URL || _global.webkitURL; |
| 192 | + const url = URL.createObjectURL(blob); |
| 193 | + if (popup) (popup.location as any) = url; |
| 194 | + else location.href = url; |
| 195 | + popup = null; // reverse-tabnabbing #460 |
| 196 | + setTimeout(function() { |
| 197 | + URL.revokeObjectURL(url); |
| 198 | + }, 4e4); // 40s |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | +export { saveAs }; |
0 commit comments