提交 | 用户 | 时间
|
820397
|
1 |
export const openWindow = ( |
H |
2 |
url: string, |
|
3 |
opt?: { |
|
4 |
target?: '_self' | '_blank' | string |
|
5 |
noopener?: boolean |
|
6 |
noreferrer?: boolean |
|
7 |
} |
|
8 |
) => { |
|
9 |
const { target = '__blank', noopener = true, noreferrer = true } = opt || {} |
|
10 |
const feature: string[] = [] |
|
11 |
|
|
12 |
noopener && feature.push('noopener=yes') |
|
13 |
noreferrer && feature.push('noreferrer=yes') |
|
14 |
|
|
15 |
window.open(url, target, feature.join(',')) |
|
16 |
} |
|
17 |
|
|
18 |
/** |
|
19 |
* @description: base64 to blob |
|
20 |
*/ |
|
21 |
export const dataURLtoBlob = (base64Buf: string): Blob => { |
|
22 |
const arr = base64Buf.split(',') |
|
23 |
const typeItem = arr[0] |
|
24 |
const mime = typeItem.match(/:(.*?);/)![1] |
|
25 |
const bstr = window.atob(arr[1]) |
|
26 |
let n = bstr.length |
|
27 |
const u8arr = new Uint8Array(n) |
|
28 |
while (n--) { |
|
29 |
u8arr[n] = bstr.charCodeAt(n) |
|
30 |
} |
|
31 |
return new Blob([u8arr], { type: mime }) |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* img url to base64 |
|
36 |
* @param url |
|
37 |
*/ |
|
38 |
export const urlToBase64 = (url: string, mineType?: string): Promise<string> => { |
|
39 |
return new Promise((resolve, reject) => { |
|
40 |
let canvas = document.createElement('CANVAS') as Nullable<HTMLCanvasElement> |
|
41 |
const ctx = canvas!.getContext('2d') |
|
42 |
|
|
43 |
const img = new Image() |
|
44 |
img.crossOrigin = '' |
|
45 |
img.onload = function () { |
|
46 |
if (!canvas || !ctx) { |
|
47 |
return reject() |
|
48 |
} |
|
49 |
canvas.height = img.height |
|
50 |
canvas.width = img.width |
|
51 |
ctx.drawImage(img, 0, 0) |
|
52 |
const dataURL = canvas.toDataURL(mineType || 'image/png') |
|
53 |
canvas = null |
|
54 |
resolve(dataURL) |
|
55 |
} |
|
56 |
img.src = url |
|
57 |
}) |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* Download online pictures |
|
62 |
* @param url |
|
63 |
* @param filename |
|
64 |
* @param mime |
|
65 |
* @param bom |
|
66 |
*/ |
|
67 |
export const downloadByOnlineUrl = ( |
|
68 |
url: string, |
|
69 |
filename: string, |
|
70 |
mime?: string, |
|
71 |
bom?: BlobPart |
|
72 |
) => { |
|
73 |
urlToBase64(url).then((base64) => { |
|
74 |
downloadByBase64(base64, filename, mime, bom) |
|
75 |
}) |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Download pictures based on base64 |
|
80 |
* @param buf |
|
81 |
* @param filename |
|
82 |
* @param mime |
|
83 |
* @param bom |
|
84 |
*/ |
|
85 |
export const downloadByBase64 = (buf: string, filename: string, mime?: string, bom?: BlobPart) => { |
|
86 |
const base64Buf = dataURLtoBlob(buf) |
|
87 |
downloadByData(base64Buf, filename, mime, bom) |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Download according to the background interface file stream |
|
92 |
* @param {*} data |
|
93 |
* @param {*} filename |
|
94 |
* @param {*} mime |
|
95 |
* @param {*} bom |
|
96 |
*/ |
|
97 |
export const downloadByData = (data: BlobPart, filename: string, mime?: string, bom?: BlobPart) => { |
|
98 |
const blobData = typeof bom !== 'undefined' ? [bom, data] : [data] |
|
99 |
const blob = new Blob(blobData, { type: mime || 'application/octet-stream' }) |
|
100 |
|
|
101 |
const blobURL = window.URL.createObjectURL(blob) |
|
102 |
const tempLink = document.createElement('a') |
|
103 |
tempLink.style.display = 'none' |
|
104 |
tempLink.href = blobURL |
|
105 |
tempLink.setAttribute('download', filename) |
|
106 |
if (typeof tempLink.download === 'undefined') { |
|
107 |
tempLink.setAttribute('target', '_blank') |
|
108 |
} |
|
109 |
document.body.appendChild(tempLink) |
|
110 |
tempLink.click() |
|
111 |
document.body.removeChild(tempLink) |
|
112 |
window.URL.revokeObjectURL(blobURL) |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Download file according to file address |
|
117 |
* @param {*} sUrl |
|
118 |
*/ |
|
119 |
export const downloadByUrl = ({ |
|
120 |
url, |
|
121 |
target = '_blank', |
|
122 |
fileName |
|
123 |
}: { |
|
124 |
url: string |
|
125 |
target?: '_self' | '_blank' |
|
126 |
fileName?: string |
|
127 |
}): boolean => { |
|
128 |
const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1 |
|
129 |
const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1 |
|
130 |
|
|
131 |
if (/(iP)/g.test(window.navigator.userAgent)) { |
|
132 |
console.error('Your browser does not support download!') |
|
133 |
return false |
|
134 |
} |
|
135 |
if (isChrome || isSafari) { |
|
136 |
const link = document.createElement('a') |
|
137 |
link.href = url |
|
138 |
link.target = target |
|
139 |
|
|
140 |
if (link.download !== undefined) { |
|
141 |
link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length) |
|
142 |
} |
|
143 |
|
|
144 |
if (document.createEvent) { |
|
145 |
const e = document.createEvent('MouseEvents') |
|
146 |
e.initEvent('click', true, true) |
|
147 |
link.dispatchEvent(e) |
|
148 |
return true |
|
149 |
} |
|
150 |
} |
|
151 |
if (url.indexOf('?') === -1) { |
|
152 |
url += '?download' |
|
153 |
} |
|
154 |
|
|
155 |
openWindow(url, { target }) |
|
156 |
return true |
|
157 |
} |