|
6 | 6 | * LICENSE file in the root directory of this source tree.
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var Lib = require('../lib'); |
| 12 | + |
9 | 13 | /*
|
10 | 14 | * substantial portions of this code from FileSaver.js
|
11 | 15 | * https://github.com/eligrey/FileSaver.js
|
|
18 | 22 | * License: MIT
|
19 | 23 | * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
|
20 | 24 | */
|
21 |
| - |
22 |
| -'use strict'; |
23 |
| - |
24 |
| -var fileSaver = function(url, name) { |
| 25 | +function fileSaver(url, name, format) { |
25 | 26 | var saveLink = document.createElement('a');
|
26 | 27 | var canUseSaveLink = 'download' in saveLink;
|
| 28 | + |
27 | 29 | var promise = new Promise(function(resolve, reject) {
|
28 |
| - // IE <10 is explicitly unsupported |
29 |
| - if(typeof navigator !== 'undefined' && /MSIE [1-9]\./.test(navigator.userAgent)) { |
| 30 | + var blob; |
| 31 | + var objectUrl; |
| 32 | + |
| 33 | + if(isIE9orBelow()) { |
30 | 34 | reject(new Error('IE < 10 unsupported'));
|
31 | 35 | }
|
32 | 36 |
|
33 |
| - // First try a.download, then web filesystem, then object URLs |
34 |
| - // Safari doesn't allow downloading of blob urls |
35 |
| - document.location.href = 'data:application/octet-stream' + url.slice(url.search(/[,;]/)); |
36 |
| - resolve(name); |
| 37 | + // Safari doesn't allow downloading of blob urls |
37 | 38 | if(Lib.isSafari()) {
|
| 39 | + document.location.href = 'data:application/octet-stream' + url; |
| 40 | + return resolve(); |
38 | 41 | }
|
39 | 42 |
|
40 |
| - if(!name) { |
41 |
| - name = 'download'; |
| 43 | + // IE 10+ (native saveAs) |
| 44 | + if(Lib.isIE()) { |
| 45 | + // At this point we are only dealing with a decoded SVG as |
| 46 | + // a data URL (since IE only supports SVG) |
| 47 | + blob = createBlob(url, format); |
| 48 | + window.navigator.msSaveBlob(blob, name); |
| 49 | + blob = null; |
| 50 | + return resolve(name); |
42 | 51 | }
|
43 | 52 |
|
44 | 53 | if(canUseSaveLink) {
|
45 |
| - saveLink.href = url; |
| 54 | + blob = createBlob(url, format); |
| 55 | + objectUrl = window.URL.createObjectURL(blob); |
| 56 | + |
| 57 | + saveLink.href = objectUrl; |
46 | 58 | saveLink.download = name;
|
47 | 59 | document.body.appendChild(saveLink);
|
48 | 60 | saveLink.click();
|
| 61 | + |
49 | 62 | document.body.removeChild(saveLink);
|
50 |
| - resolve(name); |
51 |
| - } |
| 63 | + window.URL.revokeObjectURL(objectUrl); |
| 64 | + blob = null; |
52 | 65 |
|
53 |
| - // IE 10+ (native saveAs) |
54 |
| - if(typeof navigator !== 'undefined' && navigator.msSaveBlob) { |
55 |
| - // At this point we are only dealing with a SVG encoded as |
56 |
| - // a data URL (since IE only supports SVG) |
57 |
| - var encoded = url.split(/^data:image\/svg\+xml,/)[1]; |
58 |
| - var svg = decodeURIComponent(encoded); |
59 |
| - navigator.msSaveBlob(new Blob([svg]), name); |
60 |
| - resolve(name); |
| 66 | + return resolve(name); |
61 | 67 | }
|
62 | 68 |
|
63 | 69 | reject(new Error('download error'));
|
64 | 70 | });
|
65 | 71 |
|
66 | 72 | return promise;
|
67 |
| -}; |
| 73 | +} |
| 74 | + |
| 75 | +function isIE9orBelow() { |
| 76 | + return ( |
| 77 | + Lib.isIE() && |
| 78 | + typeof window.navigator !== 'undefined' && |
| 79 | + /MSIE [1-9]\./.test(window.navigator.userAgent) |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +// Taken from https://bl.ocks.org/nolanlawson/0eac306e4dac2114c752 |
| 84 | +function fixBinary(b) { |
| 85 | + var len = b.length; |
| 86 | + var buf = new ArrayBuffer(len); |
| 87 | + var arr = new Uint8Array(buf); |
| 88 | + for(var i = 0; i < len; i++) { |
| 89 | + arr[i] = b.charCodeAt(i); |
| 90 | + } |
| 91 | + return buf; |
| 92 | +} |
| 93 | + |
| 94 | +function createBlob(url, format) { |
| 95 | + if(format === 'svg') { |
| 96 | + return new window.Blob([url]); |
| 97 | + } else { |
| 98 | + var binary = fixBinary(window.atob(url)); |
| 99 | + return new window.Blob([binary], {type: 'image/' + format}); |
| 100 | + } |
| 101 | +} |
68 | 102 |
|
69 | 103 | module.exports = fileSaver;
|
0 commit comments