FileSaver.js 5.8 KB

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