Sanakey
3 天以前 b5c1614fe473330ceca8b7cff0f1802e19bd5039
提交 | 用户 | age
ba068b 1 import { openWindow } from '..';
234c1d 2 import { dataURLtoBlob, urlToBase64 } from './base64Conver';
a161bf 3
661db0 4 /**
V 5  * Download online pictures
6  * @param url
7  * @param filename
8  * @param mime
9  * @param bom
10  */
11 export function downloadByOnlineUrl(url: string, filename: string, mime?: string, bom?: BlobPart) {
12   urlToBase64(url).then((base64) => {
13     downloadByBase64(base64, filename, mime, bom);
14   });
15 }
16
17 /**
18  * Download pictures based on base64
19  * @param buf
20  * @param filename
21  * @param mime
22  * @param bom
23  */
a161bf 24 export function downloadByBase64(buf: string, filename: string, mime?: string, bom?: BlobPart) {
V 25   const base64Buf = dataURLtoBlob(buf);
26   downloadByData(base64Buf, filename, mime, bom);
27 }
28
2f6253 29 /**
a161bf 30  * Download according to the background interface file stream
2f6253 31  * @param {*} data
32  * @param {*} filename
33  * @param {*} mime
34  * @param {*} bom
35  */
36 export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
37   const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
38   const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
a248e2 39
V 40   const blobURL = window.URL.createObjectURL(blob);
41   const tempLink = document.createElement('a');
42   tempLink.style.display = 'none';
43   tempLink.href = blobURL;
44   tempLink.setAttribute('download', filename);
45   if (typeof tempLink.download === 'undefined') {
46     tempLink.setAttribute('target', '_blank');
2f6253 47   }
a248e2 48   document.body.appendChild(tempLink);
V 49   tempLink.click();
50   document.body.removeChild(tempLink);
51   window.URL.revokeObjectURL(blobURL);
2f6253 52 }
db3092 53
2f6253 54 /**
a161bf 55  * Download file according to file address
2f6253 56  * @param {*} sUrl
57  */
58 export function downloadByUrl({
59   url,
60   target = '_blank',
61   fileName,
62 }: {
63   url: string;
64   target?: TargetContext;
65   fileName?: string;
66 }): boolean {
67   const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
68   const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
69
70   if (/(iP)/g.test(window.navigator.userAgent)) {
a161bf 71     console.error('Your browser does not support download!');
2f6253 72     return false;
73   }
74   if (isChrome || isSafari) {
75     const link = document.createElement('a');
76     link.href = url;
77     link.target = target;
78
79     if (link.download !== undefined) {
80       link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
81     }
82
83     if (document.createEvent) {
84       const e = document.createEvent('MouseEvents');
85       e.initEvent('click', true, true);
86       link.dispatchEvent(e);
87       return true;
88     }
89   }
90   if (url.indexOf('?') === -1) {
91     url += '?download';
92   }
93
ba068b 94   openWindow(url, { target });
2f6253 95   return true;
96 }