提交 | 用户 | 时间
|
820397
|
1 |
const download0 = (data: Blob, fileName: string, mineType: string) => { |
H |
2 |
// 创建 blob |
|
3 |
const blob = new Blob([data], { type: mineType }) |
|
4 |
// 创建 href 超链接,点击进行下载 |
|
5 |
window.URL = window.URL || window.webkitURL |
|
6 |
const href = URL.createObjectURL(blob) |
|
7 |
const downA = document.createElement('a') |
|
8 |
downA.href = href |
|
9 |
downA.download = fileName |
|
10 |
downA.click() |
|
11 |
// 销毁超连接 |
|
12 |
window.URL.revokeObjectURL(href) |
|
13 |
} |
|
14 |
|
|
15 |
const download = { |
|
16 |
// 下载 Excel 方法 |
|
17 |
excel: (data: Blob, fileName: string) => { |
|
18 |
download0(data, fileName, 'application/vnd.ms-excel') |
|
19 |
}, |
|
20 |
// 下载 Word 方法 |
|
21 |
word: (data: Blob, fileName: string) => { |
|
22 |
download0(data, fileName, 'application/msword') |
|
23 |
}, |
|
24 |
// 下载 Zip 方法 |
|
25 |
zip: (data: Blob, fileName: string) => { |
|
26 |
download0(data, fileName, 'application/zip') |
|
27 |
}, |
|
28 |
// 下载 Html 方法 |
|
29 |
html: (data: Blob, fileName: string) => { |
|
30 |
download0(data, fileName, 'text/html') |
|
31 |
}, |
|
32 |
// 下载 Markdown 方法 |
|
33 |
markdown: (data: Blob, fileName: string) => { |
|
34 |
download0(data, fileName, 'text/markdown') |
|
35 |
}, |
|
36 |
// 下载图片(允许跨域) |
|
37 |
image: (url: string) => { |
|
38 |
const image = new Image() |
|
39 |
image.setAttribute('crossOrigin', 'anonymous') |
|
40 |
image.src = url |
|
41 |
image.onload = () => { |
|
42 |
const canvas = document.createElement('canvas') |
|
43 |
canvas.width = image.width |
|
44 |
canvas.height = image.height |
|
45 |
const ctx = canvas.getContext('2d') as CanvasDrawImage |
|
46 |
ctx.drawImage(image, 0, 0, image.width, image.height) |
|
47 |
const url = canvas.toDataURL('image/png') |
|
48 |
const a = document.createElement('a') |
|
49 |
a.href = url |
|
50 |
a.download = 'image.png' |
|
51 |
a.click() |
|
52 |
} |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
export default download |