FileSaver.js 6.4 KB

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