1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
4 } else if (typeof exports !== "undefined") {
11 global.FileSaver = mod.exports;
18 * A saveAs() FileSaver implementation.
20 * By Eli Grey, http://eligrey.com
22 * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
23 * source : http://purl.eligrey.com/github/FileSaver.js
25 // The one and only way of getting global scope in all environments
26 // https://stackoverflow.com/q/3277182/1008999
27 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;
29 function bom(blob, opts) {
30 if (typeof opts === 'undefined') opts = {
32 }; else if (typeof opts !== 'object') {
33 console.warn('Deprecated: Expected third argument to be a object');
37 } // prepend BOM for UTF-8 XML and text/* types (including HTML)
38 // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
40 if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
41 return new Blob([String.fromCharCode(0xFEFF), blob], {
49 function download(url, name, opts) {
50 var xhr = new XMLHttpRequest();
52 xhr.responseType = 'blob';
54 xhr.onload = function () {
55 saveAs(xhr.response, name, opts);
58 xhr.onerror = function () {
59 console.error('could not download file');
65 function corsEnabled(url) {
66 var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
68 xhr.open('HEAD', url, false);
74 return xhr.status >= 200 && xhr.status <= 299;
75 } // `a.click()` doesn't work for all browsers (#465)
78 function click(node) {
80 node.dispatchEvent(new MouseEvent('click'));
82 var evt = document.createEvent('MouseEvents');
83 evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
84 node.dispatchEvent(evt);
88 var saveAs = _global.saveAs || (// probably in some web worker
89 typeof window !== 'object' || window !== _global ? function saveAs() { }
91 // Use download attribute first if possible (#193 Lumia mobile)
92 : 'download' in HTMLAnchorElement.prototype ? function saveAs(blob, name, opts) {
93 var URL = _global.URL || _global.webkitURL;
94 var a = document.createElement('a');
95 name = name || blob.name || 'download';
97 a.rel = 'noopener'; // tabnabbing
98 // TODO: detect chrome extensions & packaged apps
99 // a.target = '_blank'
101 if (typeof blob === 'string') {
102 // Support regular links
105 if (a.origin !== location.origin) {
106 corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
112 a.href = URL.createObjectURL(blob);
113 setTimeout(function () {
114 URL.revokeObjectURL(a.href);
117 setTimeout(function () {
121 } // Use msSaveOrOpenBlob as a second approach
122 : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
123 name = name || blob.name || 'download';
125 if (typeof blob === 'string') {
126 if (corsEnabled(blob)) {
127 download(blob, name, opts);
129 var a = document.createElement('a');
132 setTimeout(function () {
137 navigator.msSaveOrOpenBlob(bom(blob, opts), name);
139 } // Fallback to using FileReader and a popup
140 : function saveAs(blob, name, opts, popup) {
141 // Open a popup immediately do go around popup blocker
142 // Mostly only available on user interaction and the fileReader is async so...
143 popup = popup || open('', '_blank');
146 popup.document.title = popup.document.body.innerText = 'downloading...';
149 if (typeof blob === 'string') return download(blob, name, opts);
150 var force = blob.type === 'application/octet-stream';
152 var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
154 var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
156 if ((isChromeIOS || force && isSafari) && typeof FileReader === 'object') {
157 // Safari doesn't allow downloading of blob URLs
158 var reader = new FileReader();
160 reader.onloadend = function () {
161 var url = reader.result;
162 url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
163 if (popup) popup.location.href = url; else location = url;
164 popup = null; // reverse-tabnabbing #460
167 reader.readAsDataURL(blob);
169 var URL = _global.URL || _global.webkitURL;
170 var url = URL.createObjectURL(blob);
171 if (popup) popup.location = url; else location.href = url;
172 popup = null; // reverse-tabnabbing #460
174 setTimeout(function () {
175 URL.revokeObjectURL(url);
179 _global.saveAs = saveAs.saveAs = saveAs;
181 if (typeof module !== 'undefined') {
182 module.exports = saveAs;