FileSaver.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
  27. function bom(blob, opts) {
  28. if (typeof opts === 'undefined') opts = {
  29. autoBom: false
  30. };else if (typeof opts !== 'object') {
  31. console.warn('Deprecated: Expected third argument to be a object');
  32. opts = {
  33. autoBom: !opts
  34. };
  35. } // prepend BOM for UTF-8 XML and text/* types (including HTML)
  36. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  37. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  38. return new Blob([String.fromCharCode(0xFEFF), blob], {
  39. type: blob.type
  40. });
  41. }
  42. return blob;
  43. }
  44. function download(url, name, opts) {
  45. var xhr = new XMLHttpRequest();
  46. xhr.open('GET', url);
  47. xhr.responseType = 'blob';
  48. xhr.onload = function () {
  49. saveAs(xhr.response, name, opts);
  50. };
  51. xhr.onerror = function () {
  52. console.error('could not download file');
  53. };
  54. xhr.send();
  55. }
  56. function corsEnabled(url) {
  57. var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
  58. xhr.open('HEAD', url, false);
  59. try {
  60. xhr.send();
  61. } catch (e) {}
  62. return xhr.status >= 200 && xhr.status <= 299;
  63. } // `a.click()` doesn't work for all browsers (#465)
  64. function click(node) {
  65. try {
  66. node.dispatchEvent(new MouseEvent('click'));
  67. } catch (e) {
  68. var evt = document.createEvent('MouseEvents');
  69. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  70. node.dispatchEvent(evt);
  71. }
  72. } // Detect WebView inside a native macOS app by ruling out all browsers
  73. // We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
  74. // https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
  75. var isMacOSWebView = /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
  76. var saveAs = _global.saveAs || ( // probably in some web worker
  77. typeof window !== 'object' || window !== _global ? function saveAs() {}
  78. /* noop */
  79. // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
  80. : 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
  81. var URL = _global.URL || _global.webkitURL;
  82. var a = document.createElement('a');
  83. name = name || blob.name || 'download';
  84. a.download = name;
  85. a.rel = 'noopener'; // tabnabbing
  86. // TODO: detect chrome extensions & packaged apps
  87. // a.target = '_blank'
  88. if (typeof blob === 'string') {
  89. // Support regular links
  90. a.href = blob;
  91. if (a.origin !== location.origin) {
  92. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  93. } else {
  94. click(a);
  95. }
  96. } else {
  97. // Support blobs
  98. a.href = URL.createObjectURL(blob);
  99. setTimeout(function () {
  100. URL.revokeObjectURL(a.href);
  101. }, 4E4); // 40s
  102. setTimeout(function () {
  103. click(a);
  104. }, 0);
  105. }
  106. } // Use msSaveOrOpenBlob as a second approach
  107. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  108. name = name || blob.name || 'download';
  109. if (typeof blob === 'string') {
  110. if (corsEnabled(blob)) {
  111. download(blob, name, opts);
  112. } else {
  113. var a = document.createElement('a');
  114. a.href = blob;
  115. a.target = '_blank';
  116. setTimeout(function () {
  117. click(a);
  118. });
  119. }
  120. } else {
  121. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  122. }
  123. } // Fallback to using FileReader and a popup
  124. : function saveAs(blob, name, opts, popup) {
  125. // Open a popup immediately do go around popup blocker
  126. // Mostly only available on user interaction and the fileReader is async so...
  127. popup = popup || open('', '_blank');
  128. if (popup) {
  129. popup.document.title = popup.document.body.innerText = 'downloading...';
  130. }
  131. if (typeof blob === 'string') return download(blob, name, opts);
  132. var force = blob.type === 'application/octet-stream';
  133. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  134. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  135. if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
  136. // Safari doesn't allow downloading of blob URLs
  137. var reader = new FileReader();
  138. reader.onloadend = function () {
  139. var url = reader.result;
  140. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  141. if (popup) popup.location.href = url;else location = url;
  142. popup = null; // reverse-tabnabbing #460
  143. };
  144. reader.readAsDataURL(blob);
  145. } else {
  146. var URL = _global.URL || _global.webkitURL;
  147. var url = URL.createObjectURL(blob);
  148. if (popup) popup.location = url;else location.href = url;
  149. popup = null; // reverse-tabnabbing #460
  150. setTimeout(function () {
  151. URL.revokeObjectURL(url);
  152. }, 4E4); // 40s
  153. }
  154. });
  155. _global.saveAs = saveAs.saveAs = saveAs;
  156. if (typeof module !== 'undefined') {
  157. module.exports = saveAs;
  158. }
  159. });