FileSaver.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*! FileSaver.js
  2. * A saveAs() FileSaver implementation.
  3. * 2014-01-24
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * License: X11/MIT
  7. * See LICENSE.md
  8. */
  9. /*global self */
  10. /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
  11. /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
  12. (function() {
  13. var saveAs = saveAs
  14. // IE 10+ (native saveAs)
  15. || (typeof navigator !== "undefined" &&
  16. navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
  17. // Everyone else
  18. || (function(view) {
  19. "use strict";
  20. // IE <10 is explicitly unsupported
  21. if (typeof navigator !== "undefined" &&
  22. /MSIE [1-9]\./.test(navigator.userAgent)) {
  23. return;
  24. }
  25. var
  26. doc = view.document
  27. // only get URL when necessary in case BlobBuilder.js hasn't overridden it yet
  28. , get_URL = function() {
  29. return view.URL || view.webkitURL || view;
  30. }
  31. , URL = view.URL || view.webkitURL || view
  32. , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
  33. , can_use_save_link = !view.externalHost && "download" in save_link
  34. , click = function(node) {
  35. var event = doc.createEvent("MouseEvents");
  36. event.initMouseEvent(
  37. "click", true, false, view, 0, 0, 0, 0, 0
  38. , false, false, false, false, 0, null
  39. );
  40. node.dispatchEvent(event);
  41. }
  42. , webkit_req_fs = view.webkitRequestFileSystem
  43. , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
  44. , throw_outside = function(ex) {
  45. (view.setImmediate || view.setTimeout)(function() {
  46. throw ex;
  47. }, 0);
  48. }
  49. , force_saveable_type = "application/octet-stream"
  50. , fs_min_size = 0
  51. , deletion_queue = []
  52. , process_deletion_queue = function() {
  53. var i = deletion_queue.length;
  54. while (i--) {
  55. var file = deletion_queue[i];
  56. if (typeof file === "string") { // file is an object URL
  57. URL.revokeObjectURL(file);
  58. } else { // file is a File
  59. file.remove();
  60. }
  61. }
  62. deletion_queue.length = 0; // clear queue
  63. }
  64. , dispatch = function(filesaver, event_types, event) {
  65. event_types = [].concat(event_types);
  66. var i = event_types.length;
  67. while (i--) {
  68. var listener = filesaver["on" + event_types[i]];
  69. if (typeof listener === "function") {
  70. try {
  71. listener.call(filesaver, event || filesaver);
  72. } catch (ex) {
  73. throw_outside(ex);
  74. }
  75. }
  76. }
  77. }
  78. , FileSaver = function(blob, name) {
  79. // First try a.download, then web filesystem, then object URLs
  80. var
  81. filesaver = this
  82. , type = blob.type
  83. , blob_changed = false
  84. , object_url
  85. , target_view
  86. , get_object_url = function() {
  87. var object_url = get_URL().createObjectURL(blob);
  88. deletion_queue.push(object_url);
  89. return object_url;
  90. }
  91. , dispatch_all = function() {
  92. dispatch(filesaver, "writestart progress write writeend".split(" "));
  93. }
  94. // on any filesys errors revert to saving with object URLs
  95. , fs_error = function() {
  96. // don't create more object URLs than needed
  97. if (blob_changed || !object_url) {
  98. object_url = get_object_url(blob);
  99. }
  100. if (target_view) {
  101. target_view.location.href = object_url;
  102. } else {
  103. window.open(object_url, "_blank");
  104. }
  105. filesaver.readyState = filesaver.DONE;
  106. dispatch_all();
  107. }
  108. , abortable = function(func) {
  109. return function() {
  110. if (filesaver.readyState !== filesaver.DONE) {
  111. return func.apply(this, arguments);
  112. }
  113. };
  114. }
  115. , create_if_not_found = {create: true, exclusive: false}
  116. , slice
  117. ;
  118. filesaver.readyState = filesaver.INIT;
  119. if (!name) {
  120. name = "download";
  121. }
  122. if (can_use_save_link) {
  123. object_url = get_object_url(blob);
  124. // FF for Android has a nasty garbage collection mechanism
  125. // that turns all objects that are not pure javascript into 'deadObject'
  126. // this means `doc` and `save_link` are unusable and need to be recreated
  127. // `view` is usable though:
  128. doc = view.document;
  129. save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
  130. save_link.href = object_url;
  131. save_link.download = name;
  132. var event = doc.createEvent("MouseEvents");
  133. event.initMouseEvent(
  134. "click", true, false, view, 0, 0, 0, 0, 0
  135. , false, false, false, false, 0, null
  136. );
  137. save_link.dispatchEvent(event);
  138. filesaver.readyState = filesaver.DONE;
  139. dispatch_all();
  140. return;
  141. }
  142. // Object and web filesystem URLs have a problem saving in Google Chrome when
  143. // viewed in a tab, so I force save with application/octet-stream
  144. // http://code.google.com/p/chromium/issues/detail?id=91158
  145. if (view.chrome && type && type !== force_saveable_type) {
  146. slice = blob.slice || blob.webkitSlice;
  147. blob = slice.call(blob, 0, blob.size, force_saveable_type);
  148. blob_changed = true;
  149. }
  150. // Since I can't be sure that the guessed media type will trigger a download
  151. // in WebKit, I append .download to the filename.
  152. // https://bugs.webkit.org/show_bug.cgi?id=65440
  153. if (webkit_req_fs && name !== "download") {
  154. name += ".download";
  155. }
  156. if (type === force_saveable_type || webkit_req_fs) {
  157. target_view = view;
  158. }
  159. if (!req_fs) {
  160. fs_error();
  161. return;
  162. }
  163. fs_min_size += blob.size;
  164. req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
  165. fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
  166. var save = function() {
  167. dir.getFile(name, create_if_not_found, abortable(function(file) {
  168. file.createWriter(abortable(function(writer) {
  169. writer.onwriteend = function(event) {
  170. target_view.location.href = file.toURL();
  171. deletion_queue.push(file);
  172. filesaver.readyState = filesaver.DONE;
  173. dispatch(filesaver, "writeend", event);
  174. };
  175. writer.onerror = function() {
  176. var error = writer.error;
  177. if (error.code !== error.ABORT_ERR) {
  178. fs_error();
  179. }
  180. };
  181. "writestart progress write abort".split(" ").forEach(function(event) {
  182. writer["on" + event] = filesaver["on" + event];
  183. });
  184. writer.write(blob);
  185. filesaver.abort = function() {
  186. writer.abort();
  187. filesaver.readyState = filesaver.DONE;
  188. };
  189. filesaver.readyState = filesaver.WRITING;
  190. }), fs_error);
  191. }), fs_error);
  192. };
  193. dir.getFile(name, {create: false}, abortable(function(file) {
  194. // delete file if it already exists
  195. file.remove();
  196. save();
  197. }), abortable(function(ex) {
  198. if (ex.code === ex.NOT_FOUND_ERR) {
  199. save();
  200. } else {
  201. fs_error();
  202. }
  203. }));
  204. }), fs_error);
  205. }), fs_error);
  206. }
  207. , FS_proto = FileSaver.prototype
  208. , saveAs = function(blob, name) {
  209. return new FileSaver(blob, name);
  210. }
  211. ;
  212. FS_proto.abort = function() {
  213. var filesaver = this;
  214. filesaver.readyState = filesaver.DONE;
  215. dispatch(filesaver, "abort");
  216. };
  217. FS_proto.readyState = FS_proto.INIT = 0;
  218. FS_proto.WRITING = 1;
  219. FS_proto.DONE = 2;
  220. FS_proto.error =
  221. FS_proto.onwritestart =
  222. FS_proto.onprogress =
  223. FS_proto.onwrite =
  224. FS_proto.onabort =
  225. FS_proto.onerror =
  226. FS_proto.onwriteend =
  227. null;
  228. view.addEventListener("unload", process_deletion_queue, false);
  229. saveAs.unload = function() {
  230. process_deletion_queue();
  231. view.removeEventListener("unload", process_deletion_queue, false);
  232. };
  233. return saveAs;
  234. }(
  235. typeof self !== "undefined" && self
  236. || typeof window !== "undefined" && window
  237. || this.content
  238. ));
  239. // `self` is undefined in Firefox for Android content script context
  240. // while `this` is nsIContentFrameMessageManager
  241. // with an attribute `content` that corresponds to the window
  242. o2.saveAs = saveAs;
  243. if (typeof module !== "undefined") module.exports = saveAs;
  244. })(this);