2 * A saveAs() FileSaver implementation.
6 * By Eli Grey, http://eligrey.com
8 * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
12 /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
14 /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
16 var saveAs = saveAs || (function(view) {
18 // IE <10 is explicitly unsupported
19 if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
24 // only get URL when necessary in case Blob.js hasn't overridden it yet
25 , get_URL = function() {
26 return view.URL || view.webkitURL || view;
28 , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
29 , can_use_save_link = "download" in save_link
30 , click = function(node) {
31 var event = new MouseEvent("click");
32 node.dispatchEvent(event);
34 , is_safari = /constructor/i.test(view.HTMLElement) || view.safari
35 , is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
36 , throw_outside = function(ex) {
37 (view.setImmediate || view.setTimeout)(function() {
41 , force_saveable_type = "application/octet-stream"
42 // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
43 , arbitrary_revoke_timeout = 1000 * 40 // in ms
44 , revoke = function(file) {
45 var revoker = function() {
46 if (typeof file === "string") { // file is an object URL
47 get_URL().revokeObjectURL(file);
48 } else { // file is a File
52 setTimeout(revoker, arbitrary_revoke_timeout);
54 , dispatch = function(filesaver, event_types, event) {
55 event_types = [].concat(event_types);
56 var i = event_types.length;
58 var listener = filesaver["on" + event_types[i]];
59 if (typeof listener === "function") {
61 listener.call(filesaver, event || filesaver);
68 , auto_bom = function(blob) {
69 // prepend BOM for UTF-8 XML and text/* types (including HTML)
70 // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
71 if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
72 return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
76 , FileSaver = function(blob, name, no_auto_bom) {
78 blob = auto_bom(blob);
80 // First try a.download, then web filesystem, then object URLs
84 , force = type === force_saveable_type
86 , dispatch_all = function() {
87 dispatch(filesaver, "writestart progress write writeend".split(" "));
89 // on any filesys errors revert to saving with object URLs
90 , fs_error = function() {
91 if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
92 // Safari doesn't allow downloading of blob urls
93 var reader = new FileReader();
94 reader.onloadend = function() {
95 var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
96 var popup = view.open(url, '_blank');
97 if(!popup) view.location.href = url;
98 url=undefined; // release reference before dispatching
99 filesaver.readyState = filesaver.DONE;
102 reader.readAsDataURL(blob);
103 filesaver.readyState = filesaver.INIT;
106 // don't create more object URLs than needed
108 object_url = get_URL().createObjectURL(blob);
111 view.location.href = object_url;
113 var opened = view.open(object_url, "_blank");
115 // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
116 view.location.href = object_url;
119 filesaver.readyState = filesaver.DONE;
124 filesaver.readyState = filesaver.INIT;
126 if (can_use_save_link) {
127 object_url = get_URL().createObjectURL(blob);
128 setTimeout(function() {
129 save_link.href = object_url;
130 save_link.download = name;
134 filesaver.readyState = filesaver.DONE;
141 , FS_proto = FileSaver.prototype
142 , saveAs = function(blob, name, no_auto_bom) {
143 return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
146 // IE 10+ (native saveAs)
147 if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
148 return function(blob, name, no_auto_bom) {
149 name = name || blob.name || "download";
152 blob = auto_bom(blob);
154 return navigator.msSaveOrOpenBlob(blob, name);
158 FS_proto.abort = function(){};
159 FS_proto.readyState = FS_proto.INIT = 0;
160 FS_proto.WRITING = 1;
164 FS_proto.onwritestart =
165 FS_proto.onprogress =
169 FS_proto.onwriteend =
174 typeof self !== "undefined" && self
175 || typeof window !== "undefined" && window
178 // `self` is undefined in Firefox for Android content script context
179 // while `this` is nsIContentFrameMessageManager
180 // with an attribute `content` that corresponds to the window
182 if (typeof module !== "undefined" && module.exports) {
183 module.exports.saveAs = saveAs;
184 } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
185 define("FileSaver.js", function() {