FileSaver.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.FileSaver = mod.exports;
  12. }
  13. })(this, function () {
  14. "use strict";
  15. /*
  16. * FileSaver.js
  17. * A saveAs() FileSaver implementation.
  18. *
  19. * By Eli Grey, http://eligrey.com
  20. *
  21. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  22. * source : http://purl.eligrey.com/github/FileSaver.js
  23. */
  24. // The one and only way of getting global scope in all environments
  25. // https://stackoverflow.com/q/3277182/1008999
  26. var _global = function () {
  27. // some use content security policy to disable eval
  28. try {
  29. return Function('return this')() || (42, eval)('this');
  30. } catch (e) {
  31. // every global should have circular reference
  32. // used for checking if someone writes var window = {}; var self = {}
  33. return typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : this;
  34. }
  35. }();
  36. function bom(blob, opts) {
  37. if (typeof opts === 'undefined') opts = {
  38. autoBom: false
  39. };else if (typeof opts !== 'object') {
  40. console.warn('Depricated: Expected third argument to be a object');
  41. opts = {
  42. autoBom: !opts
  43. };
  44. } // prepend BOM for UTF-8 XML and text/* types (including HTML)
  45. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  46. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  47. return new Blob([String.fromCharCode(0xFEFF), blob], {
  48. type: blob.type
  49. });
  50. }
  51. return blob;
  52. }
  53. function download(url, name, opts) {
  54. var xhr = new XMLHttpRequest();
  55. xhr.open('GET', url);
  56. xhr.responseType = 'blob';
  57. xhr.onload = function () {
  58. saveAs(xhr.response, name, opts);
  59. };
  60. xhr.onerror = function () {
  61. console.error('could not download file');
  62. };
  63. xhr.send();
  64. }
  65. function corsEnabled(url) {
  66. var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
  67. xhr.open('HEAD', url, false);
  68. xhr.send();
  69. return xhr.status >= 200 && xhr.status <= 299;
  70. } // `a.click()` doesn't work for all browsers (#465)
  71. function click(node) {
  72. try {
  73. node.dispatchEvent(new MouseEvent('click'));
  74. } catch (e) {
  75. var evt = document.createEvent('MouseEvents');
  76. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  77. node.dispatchEvent(evt);
  78. }
  79. }
  80. var saveAs = _global.saveAs || // probably in some web worker
  81. typeof window !== 'object' || window !== _global ? function saveAs() {}
  82. /* noop */
  83. // Use download attribute first if possible (#193 Lumia mobile)
  84. : 'download' in HTMLAnchorElement.prototype ? function saveAs(blob, name, opts) {
  85. var URL = _global.URL || _global.webkitURL;
  86. var a = document.createElement('a');
  87. name = name || blob.name || 'download';
  88. a.download = name;
  89. a.rel = 'noopener'; // tabnabbing
  90. // TODO: detect chrome extensions & packaged apps
  91. // a.target = '_blank'
  92. if (typeof blob === 'string') {
  93. // Support regular links
  94. a.href = blob;
  95. if (a.origin !== location.origin) {
  96. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  97. } else {
  98. click(a);
  99. }
  100. } else {
  101. // Support blobs
  102. a.href = URL.createObjectURL(blob);
  103. setTimeout(function () {
  104. URL.revokeObjectURL(a.href);
  105. }, 4E4); // 40s
  106. setTimeout(function () {
  107. click(a);
  108. }, 0);
  109. }
  110. } // Use msSaveOrOpenBlob as a second approach
  111. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  112. name = name || blob.name || 'download';
  113. if (typeof blob === 'string') {
  114. if (corsEnabled(blob)) {
  115. download(blob, name, opts);
  116. } else {
  117. var a = document.createElement('a');
  118. a.href = blob;
  119. a.target = '_blank';
  120. setTimeout(function () {
  121. click(a);
  122. });
  123. }
  124. } else {
  125. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  126. }
  127. } // Fallback to using FileReader and a popup
  128. : function saveAs(blob, name, opts, popup) {
  129. // Open a popup immediately do go around popup blocker
  130. // Mostly only avalible on user interaction and the fileReader is async so...
  131. popup = popup || open('', '_blank');
  132. if (popup) {
  133. popup.document.title = popup.document.body.innerText = 'downloading...';
  134. }
  135. if (typeof blob === 'string') return download(blob, name, opts);
  136. var force = blob.type === 'application/octet-stream';
  137. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  138. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  139. if ((isChromeIOS || force && isSafari) && typeof FileReader === 'object') {
  140. // Safari doesn't allow downloading of blob urls
  141. var reader = new FileReader();
  142. reader.onloadend = function () {
  143. var url = reader.result;
  144. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  145. if (popup) popup.location.href = url;else location = url;
  146. popup = null; // reverse-tabnabbing #460
  147. };
  148. reader.readAsDataURL(blob);
  149. } else {
  150. var URL = _global.URL || _global.webkitURL;
  151. var url = URL.createObjectURL(blob);
  152. if (popup) popup.location = url;else location.href = url;
  153. popup = null; // reverse-tabnabbing #460
  154. setTimeout(function () {
  155. URL.revokeObjectURL(url);
  156. }, 4E4); // 40s
  157. }
  158. };
  159. _global.saveAs = saveAs.saveAs = saveAs;
  160. if (typeof module !== 'undefined') {
  161. module.exports = saveAs;
  162. }
  163. });