Skip to content

Commit 09b2190

Browse files
committed
feat: FileUpload 组件文件列表增加下载功能
1 parent a3f016c commit 09b2190

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/components/FileUpload/index.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ const onExceed: UploadProps['onExceed'] = () => {
5858
const onSuccess: UploadProps['onSuccess'] = (res, file, fileList) => {
5959
emits('onSuccess', res, file, fileList)
6060
}
61+
62+
const onPreview: UploadProps['onPreview'] = (e) => {
63+
const getBlob = (url: string) => new Promise((resolve) => {
64+
const xhr = new XMLHttpRequest()
65+
xhr.open('GET', url, true)
66+
xhr.responseType = 'blob'
67+
xhr.onload = () => {
68+
if (xhr.status === 200) {
69+
resolve(xhr.response)
70+
}
71+
}
72+
xhr.send()
73+
})
74+
getBlob(e.url!).then((blob: any) => {
75+
const a = document.createElement('a')
76+
const url = window.URL.createObjectURL(blob)
77+
const event = new MouseEvent('click')
78+
a.target = '_blank'
79+
a.download = e.name
80+
a.href = url
81+
a.dispatchEvent(event)
82+
})
83+
}
6184
</script>
6285

6386
<template>
@@ -69,6 +92,7 @@ const onSuccess: UploadProps['onSuccess'] = (res, file, fileList) => {
6992
:before-upload="beforeUpload"
7093
:on-exceed="onExceed"
7194
:on-success="onSuccess"
95+
:on-preview="onPreview"
7296
:http-request="httpRequest"
7397
:file-list="files"
7498
:limit="max"

0 commit comments

Comments
 (0)