FileSaver.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  73. var saveAs = _global.saveAs || ( // probably in some web worker
  74. typeof window !== 'object' || window !== _global ? function saveAs() {}
  75. /* noop */
  76. // Use download attribute first if possible (#193 Lumia mobile)
  77. : 'download' in HTMLAnchorElement.prototype ? function saveAs(blob, name, opts) {
  78. var URL = _global.URL || _global.webkitURL;
  79. var a = document.createElement('a');
  80. name = name || blob.name || 'download';
  81. a.download = name;
  82. a.rel = 'noopener'; // tabnabbing
  83. // TODO: detect chrome extensions & packaged apps
  84. // a.target = '_blank'
  85. if (typeof blob === 'string') {
  86. // Support regular links
  87. a.href = blob;
  88. if (a.origin !== location.origin) {
  89. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  90. } else {
  91. click(a);
  92. }
  93. } else {
  94. // Support blobs
  95. a.href = URL.createObjectURL(blob);
  96. setTimeout(function () {
  97. URL.revokeObjectURL(a.href);
  98. }, 4E4); // 40s
  99. setTimeout(function () {
  100. click(a);
  101. }, 0);
  102. }
  103. } // Use msSaveOrOpenBlob as a second approach
  104. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  105. name = name || blob.name || 'download';
  106. if (typeof blob === 'string') {
  107. if (corsEnabled(blob)) {
  108. download(blob, name, opts);
  109. } else {
  110. var a = document.createElement('a');
  111. a.href = blob;
  112. a.target = '_blank';
  113. setTimeout(function () {
  114. click(a);
  115. });
  116. }
  117. } else {
  118. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  119. }
  120. } // Fallback to using FileReader and a popup
  121. : function saveAs(blob, name, opts, popup) {
  122. // Open a popup immediately do go around popup blocker
  123. // Mostly only available on user interaction and the fileReader is async so...
  124. popup = popup || open('', '_blank');
  125. if (popup) {
  126. popup.document.title = popup.document.body.innerText = 'downloading...';
  127. }
  128. if (typeof blob === 'string') return download(blob, name, opts);
  129. var force = blob.type === 'application/octet-stream';
  130. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  131. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  132. if ((isChromeIOS || force && isSafari) && typeof FileReader === 'object') {
  133. // Safari doesn't allow downloading of blob URLs
  134. var reader = new FileReader();
  135. reader.onloadend = function () {
  136. var url = reader.result;
  137. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  138. if (popup) popup.location.href = url;else location = url;
  139. popup = null; // reverse-tabnabbing #460
  140. };
  141. reader.readAsDataURL(blob);
  142. } else {
  143. var URL = _global.URL || _global.webkitURL;
  144. var url = URL.createObjectURL(blob);
  145. if (popup) popup.location = url;else location.href = url;
  146. popup = null; // reverse-tabnabbing #460
  147. setTimeout(function () {
  148. URL.revokeObjectURL(url);
  149. }, 4E4); // 40s
  150. }
  151. });
  152. _global.saveAs = saveAs.saveAs = saveAs;
  153. if (typeof module !== 'undefined') {
  154. module.exports = saveAs;
  155. }
  156. });