equal
deleted
inserted
replaced
1 /* FileSaver.js |
1 /* FileSaver.js |
2 * A saveAs() FileSaver implementation. |
2 * A saveAs() FileSaver implementation. |
3 * 2014-12-17 |
3 * 2015-05-07.2 |
4 * |
4 * |
5 * By Eli Grey, http://eligrey.com |
5 * By Eli Grey, http://eligrey.com |
6 * License: X11/MIT |
6 * License: X11/MIT |
7 * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md |
7 * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md |
8 */ |
8 */ |
10 /*global self */ |
10 /*global self */ |
11 /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ |
11 /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ |
12 |
12 |
13 /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */ |
13 /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */ |
14 |
14 |
15 var saveAs = saveAs |
15 var saveAs = saveAs || (function(view) { |
16 // IE 10+ (native saveAs) |
|
17 || (typeof navigator !== "undefined" && |
|
18 navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator)) |
|
19 // Everyone else |
|
20 || (function(view) { |
|
21 "use strict"; |
16 "use strict"; |
22 // IE <10 is explicitly unsupported |
17 // IE <10 is explicitly unsupported |
23 if (typeof navigator !== "undefined" && |
18 if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) { |
24 /MSIE [1-9]\./.test(navigator.userAgent)) { |
|
25 return; |
19 return; |
26 } |
20 } |
27 var |
21 var |
28 doc = view.document |
22 doc = view.document |
29 // only get URL when necessary in case Blob.js hasn't overridden it yet |
23 // only get URL when necessary in case Blob.js hasn't overridden it yet |
79 throw_outside(ex); |
73 throw_outside(ex); |
80 } |
74 } |
81 } |
75 } |
82 } |
76 } |
83 } |
77 } |
|
78 , auto_bom = function(blob) { |
|
79 // prepend BOM for UTF-8 XML and text/* types (including HTML) |
|
80 if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { |
|
81 return new Blob(["\ufeff", blob], {type: blob.type}); |
|
82 } |
|
83 return blob; |
|
84 } |
84 , FileSaver = function(blob, name) { |
85 , FileSaver = function(blob, name) { |
|
86 blob = auto_bom(blob); |
85 // First try a.download, then web filesystem, then object URLs |
87 // First try a.download, then web filesystem, then object URLs |
86 var |
88 var |
87 filesaver = this |
89 filesaver = this |
88 , type = blob.type |
90 , type = blob.type |
89 , blob_changed = false |
91 , blob_changed = false |
205 , FS_proto = FileSaver.prototype |
207 , FS_proto = FileSaver.prototype |
206 , saveAs = function(blob, name) { |
208 , saveAs = function(blob, name) { |
207 return new FileSaver(blob, name); |
209 return new FileSaver(blob, name); |
208 } |
210 } |
209 ; |
211 ; |
|
212 // IE 10+ (native saveAs) |
|
213 if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) { |
|
214 return function(blob, name) { |
|
215 return navigator.msSaveOrOpenBlob(auto_bom(blob), name); |
|
216 }; |
|
217 } |
|
218 |
210 FS_proto.abort = function() { |
219 FS_proto.abort = function() { |
211 var filesaver = this; |
220 var filesaver = this; |
212 filesaver.readyState = filesaver.DONE; |
221 filesaver.readyState = filesaver.DONE; |
213 dispatch(filesaver, "abort"); |
222 dispatch(filesaver, "abort"); |
214 }; |
223 }; |
234 // `self` is undefined in Firefox for Android content script context |
243 // `self` is undefined in Firefox for Android content script context |
235 // while `this` is nsIContentFrameMessageManager |
244 // while `this` is nsIContentFrameMessageManager |
236 // with an attribute `content` that corresponds to the window |
245 // with an attribute `content` that corresponds to the window |
237 |
246 |
238 if (typeof module !== "undefined" && module.exports) { |
247 if (typeof module !== "undefined" && module.exports) { |
239 module.exports = saveAs; |
248 module.exports.saveAs = saveAs; |
240 } else if ((typeof define !== "undefined" && define !== null) && (define.amd != null)) { |
249 } else if ((typeof define !== "undefined" && define !== null) && (define.amd != null)) { |
241 define([], function() { |
250 define([], function() { |
242 return saveAs; |
251 return saveAs; |
243 }); |
252 }); |
244 } |
253 } |