|
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 | +var helpers = require('./helpers'); |
| 13 | + |
9 | 14 | /*
|
10 | 15 | * substantial portions of this code from FileSaver.js
|
11 | 16 | * https://github.com/eligrey/FileSaver.js
|
|
18 | 23 | * License: MIT
|
19 | 24 | * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
|
20 | 25 | */
|
21 |
| - |
22 |
| -'use strict'; |
23 |
| - |
24 |
| -var fileSaver = function(url, name) { |
| 26 | +function fileSaver(url, name, format) { |
25 | 27 | var saveLink = document.createElement('a');
|
26 | 28 | var canUseSaveLink = 'download' in saveLink;
|
27 |
| - var isSafari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent); |
| 29 | + |
28 | 30 | var promise = new Promise(function(resolve, reject) {
|
29 |
| - // IE <10 is explicitly unsupported |
30 |
| - if(typeof navigator !== 'undefined' && /MSIE [1-9]\./.test(navigator.userAgent)) { |
| 31 | + var blob; |
| 32 | + var objectUrl; |
| 33 | + |
| 34 | + if(Lib.isIE9orBelow()) { |
31 | 35 | reject(new Error('IE < 10 unsupported'));
|
32 | 36 | }
|
33 | 37 |
|
34 |
| - // First try a.download, then web filesystem, then object URLs |
35 |
| - if(isSafari) { |
36 |
| - // Safari doesn't allow downloading of blob urls |
37 |
| - document.location.href = 'data:application/octet-stream' + url.slice(url.search(/[,;]/)); |
38 |
| - resolve(name); |
| 38 | + // Safari doesn't allow downloading of blob urls |
| 39 | + if(Lib.isSafari()) { |
| 40 | + var prefix = format === 'svg' ? ',' : ';base64,'; |
| 41 | + helpers.octetStream(prefix + encodeURIComponent(url)); |
| 42 | + return resolve(name); |
39 | 43 | }
|
40 | 44 |
|
41 |
| - if(!name) { |
42 |
| - name = 'download'; |
| 45 | + // IE 10+ (native saveAs) |
| 46 | + if(Lib.isIE()) { |
| 47 | + // At this point we are only dealing with a decoded SVG as |
| 48 | + // a data URL (since IE only supports SVG) |
| 49 | + blob = helpers.createBlob(url, 'svg'); |
| 50 | + window.navigator.msSaveBlob(blob, name); |
| 51 | + blob = null; |
| 52 | + return resolve(name); |
43 | 53 | }
|
44 | 54 |
|
45 | 55 | if(canUseSaveLink) {
|
46 |
| - saveLink.href = url; |
| 56 | + blob = helpers.createBlob(url, format); |
| 57 | + objectUrl = helpers.createObjectURL(blob); |
| 58 | + |
| 59 | + saveLink.href = objectUrl; |
47 | 60 | saveLink.download = name;
|
48 | 61 | document.body.appendChild(saveLink);
|
49 | 62 | saveLink.click();
|
| 63 | + |
50 | 64 | document.body.removeChild(saveLink);
|
51 |
| - resolve(name); |
52 |
| - } |
| 65 | + helpers.revokeObjectURL(objectUrl); |
| 66 | + blob = null; |
53 | 67 |
|
54 |
| - // IE 10+ (native saveAs) |
55 |
| - if(typeof navigator !== 'undefined' && navigator.msSaveBlob) { |
56 |
| - // At this point we are only dealing with a SVG encoded as |
57 |
| - // a data URL (since IE only supports SVG) |
58 |
| - var encoded = url.split(/^data:image\/svg\+xml,/)[1]; |
59 |
| - var svg = decodeURIComponent(encoded); |
60 |
| - navigator.msSaveBlob(new Blob([svg]), name); |
61 |
| - resolve(name); |
| 68 | + return resolve(name); |
62 | 69 | }
|
63 | 70 |
|
64 | 71 | reject(new Error('download error'));
|
65 | 72 | });
|
66 | 73 |
|
67 | 74 | return promise;
|
68 |
| -}; |
| 75 | +} |
| 76 | + |
69 | 77 |
|
70 | 78 | module.exports = fileSaver;
|
0 commit comments