You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

244 lines
7.3 KiB

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