FileSaver.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* FileSaver.js
  2. * A saveAs() FileSaver implementation.
  3. * 1.3.0
  4. *
  5. * By Eli Grey, http://eligrey.com
  6. * License: 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 || (function(view) {
  13. "use strict";
  14. // IE <10 is explicitly unsupported
  15. if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
  16. return;
  17. }
  18. var
  19. doc = view.document
  20. // only get URL when necessary in case Blob.js hasn't overridden it yet
  21. , get_URL = function() {
  22. return view.URL || view.webkitURL || view;
  23. }
  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 = new MouseEvent("click");
  28. node.dispatchEvent(event);
  29. }
  30. , is_safari = /constructor/i.test(view.HTMLElement)
  31. , throw_outside = function(ex) {
  32. (view.setImmediate || view.setTimeout)(function() {
  33. throw ex;
  34. }, 0);
  35. }
  36. , force_saveable_type = "application/octet-stream"
  37. // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
  38. , arbitrary_revoke_timeout = 1000 * 40 // in ms
  39. , revoke = function(file) {
  40. var revoker = function() {
  41. if (typeof file === "string") { // file is an object URL
  42. get_URL().revokeObjectURL(file);
  43. } else { // file is a File
  44. file.remove();
  45. }
  46. };
  47. setTimeout(revoker, arbitrary_revoke_timeout);
  48. }
  49. , dispatch = function(filesaver, event_types, event) {
  50. event_types = [].concat(event_types);
  51. var i = event_types.length;
  52. while (i--) {
  53. var listener = filesaver["on" + event_types[i]];
  54. if (typeof listener === "function") {
  55. try {
  56. listener.call(filesaver, event || filesaver);
  57. } catch (ex) {
  58. throw_outside(ex);
  59. }
  60. }
  61. }
  62. }
  63. , auto_bom = function(blob) {
  64. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  65. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  66. if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  67. return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
  68. }
  69. return blob;
  70. }
  71. , FileSaver = function(blob, name, no_auto_bom) {
  72. if (!no_auto_bom) {
  73. blob = auto_bom(blob);
  74. }
  75. // First try a.download, then web filesystem, then object URLs
  76. var
  77. filesaver = this
  78. , type = blob.type
  79. , force = type === force_saveable_type
  80. , object_url
  81. , dispatch_all = function() {
  82. dispatch(filesaver, "writestart progress write writeend".split(" "));
  83. }
  84. // on any filesys errors revert to saving with object URLs
  85. , fs_error = function() {
  86. if (force && is_safari && view.FileReader) {
  87. // Safari doesn't allow downloading of blob urls
  88. var reader = new FileReader();
  89. reader.onloadend = function() {
  90. var base64Data = reader.result;
  91. view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/));
  92. filesaver.readyState = filesaver.DONE;
  93. dispatch_all();
  94. };
  95. reader.readAsDataURL(blob);
  96. filesaver.readyState = filesaver.INIT;
  97. return;
  98. }
  99. // don't create more object URLs than needed
  100. if (!object_url) {
  101. object_url = get_URL().createObjectURL(blob);
  102. }
  103. if (force) {
  104. view.location.href = object_url;
  105. } else {
  106. var opened = view.open(object_url, "_blank");
  107. if (!opened) {
  108. // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
  109. view.location.href = object_url;
  110. }
  111. }
  112. filesaver.readyState = filesaver.DONE;
  113. dispatch_all();
  114. revoke(object_url);
  115. }
  116. ;
  117. filesaver.readyState = filesaver.INIT;
  118. if (can_use_save_link) {
  119. object_url = get_URL().createObjectURL(blob);
  120. setTimeout(function() {
  121. save_link.href = object_url;
  122. save_link.download = name;
  123. click(save_link);
  124. dispatch_all();
  125. revoke(object_url);
  126. filesaver.readyState = filesaver.DONE;
  127. });
  128. return;
  129. }
  130. fs_error();
  131. }
  132. , FS_proto = FileSaver.prototype
  133. , saveAs = function(blob, name, no_auto_bom) {
  134. return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
  135. }
  136. ;
  137. // IE 10+ (native saveAs)
  138. if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
  139. return function(blob, name, no_auto_bom) {
  140. name = name || blob.name || "download";
  141. if (!no_auto_bom) {
  142. blob = auto_bom(blob);
  143. }
  144. return navigator.msSaveOrOpenBlob(blob, name);
  145. };
  146. }
  147. FS_proto.abort = function(){};
  148. FS_proto.readyState = FS_proto.INIT = 0;
  149. FS_proto.WRITING = 1;
  150. FS_proto.DONE = 2;
  151. FS_proto.error =
  152. FS_proto.onwritestart =
  153. FS_proto.onprogress =
  154. FS_proto.onwrite =
  155. FS_proto.onabort =
  156. FS_proto.onerror =
  157. FS_proto.onwriteend =
  158. null;
  159. return saveAs;
  160. }(
  161. typeof self !== "undefined" && self
  162. || typeof window !== "undefined" && window
  163. || this.content
  164. ));
  165. // `self` is undefined in Firefox for Android content script context
  166. // while `this` is nsIContentFrameMessageManager
  167. // with an attribute `content` that corresponds to the window
  168. if (typeof module !== "undefined" && module.exports) {
  169. module.exports.saveAs = saveAs;
  170. } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
  171. define([], function() {
  172. return saveAs;
  173. });
  174. }