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