blob.js
1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const resolveBlobData = blobData => {
return new Promise((resolve, reject) => {
if (blobData.type === 'application/msexcel') {
resolve(blobData);
} else if (blobData.type === 'application/json') {
const fr = new FileReader();
fr.onload = function() {
const rs = JSON.parse(this.result);
reject(rs);
};
fr.readAsText(blobData);
} else {
reject();
}
});
};
const downloadBlob = (blob, name) => {
const URL = window.URL || window.webkitURL;
const blobUrl = URL.createObjectURL(blob);
const anchor = document.createElement('a');
if ('download' in anchor) {
anchor.style.visibility = 'hidden';
anchor.href = blobUrl;
anchor.download = name;
document.body.appendChild(anchor);
let evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
anchor.dispatchEvent(evt);
document.body.removeChild(anchor);
} else if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, name);
} else {
location.href = blobUrl;
}
};
const dataURL2Blob = dataUrl => {
let arr = dataUrl.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let dataStr = atob(arr[1]);
let n = dataStr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = dataStr.charCodeAt(n);
}
return new Blob([u8arr], {type: mime });
};
export default {
resolveBlobData,
downloadBlob,
dataURL2Blob
};