From bd5c921bca6fc11739848524e91c9c8455dcdbc5 Mon Sep 17 00:00:00 2001 From: Hooray Hu <304327508@qq.com> Date: Wed, 31 Jul 2024 13:53:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20FileUpload=20=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=97=E8=A1=A8=E5=A2=9E=E5=8A=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/FileUpload/index.vue | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/components/FileUpload/index.vue b/src/components/FileUpload/index.vue index ec59ada..19b8e01 100755 --- a/src/components/FileUpload/index.vue +++ b/src/components/FileUpload/index.vue @@ -58,6 +58,29 @@ const onExceed: UploadProps['onExceed'] = () => { const onSuccess: UploadProps['onSuccess'] = (res, file, fileList) => { emits('onSuccess', res, file, fileList) } + +const onPreview: UploadProps['onPreview'] = (e) => { + const getBlob = (url: string) => new Promise((resolve) => { + const xhr = new XMLHttpRequest() + xhr.open('GET', url, true) + xhr.responseType = 'blob' + xhr.onload = () => { + if (xhr.status === 200) { + resolve(xhr.response) + } + } + xhr.send() + }) + getBlob(e.url!).then((blob: any) => { + const a = document.createElement('a') + const url = window.URL.createObjectURL(blob) + const event = new MouseEvent('click') + a.target = '_blank' + a.download = e.name + a.href = url + a.dispatchEvent(event) + }) +}