1 | module.exports = function(data, filename, mime, bom) {
|
---|
2 | var blobData = (typeof bom !== 'undefined') ? [bom, data] : [data]
|
---|
3 | var blob = new Blob(blobData, {type: mime || 'application/octet-stream'});
|
---|
4 | if (typeof window.navigator.msSaveBlob !== 'undefined') {
|
---|
5 | // IE workaround for "HTML7007: One or more blob URLs were
|
---|
6 | // revoked by closing the blob for which they were created.
|
---|
7 | // These URLs will no longer resolve as the data backing
|
---|
8 | // the URL has been freed."
|
---|
9 | window.navigator.msSaveBlob(blob, filename);
|
---|
10 | }
|
---|
11 | else {
|
---|
12 | var blobURL = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob);
|
---|
13 | var tempLink = document.createElement('a');
|
---|
14 | tempLink.style.display = 'none';
|
---|
15 | tempLink.href = blobURL;
|
---|
16 | tempLink.setAttribute('download', filename);
|
---|
17 |
|
---|
18 | // Safari thinks _blank anchor are pop ups. We only want to set _blank
|
---|
19 | // target if the browser does not support the HTML5 download attribute.
|
---|
20 | // This allows you to download files in desktop safari if pop up blocking
|
---|
21 | // is enabled.
|
---|
22 | if (typeof tempLink.download === 'undefined') {
|
---|
23 | tempLink.setAttribute('target', '_blank');
|
---|
24 | }
|
---|
25 |
|
---|
26 | document.body.appendChild(tempLink);
|
---|
27 | tempLink.click();
|
---|
28 |
|
---|
29 | // Fixes "webkit blob resource error 1"
|
---|
30 | setTimeout(function() {
|
---|
31 | document.body.removeChild(tempLink);
|
---|
32 | window.URL.revokeObjectURL(blobURL);
|
---|
33 | }, 200)
|
---|
34 | }
|
---|
35 | }
|
---|