| | |
| | | |
| | | import { IconifyIcon } from '@vben/icons'; |
| | | import { $t } from '@vben/locales'; |
| | | import { checkFileType, isFunction, isObject, isString } from '@vben/utils'; |
| | | import { checkFileType, isFunction, isImage, isObject, isString } from '@vben/utils'; |
| | | |
| | | import { Button, message, Upload } from 'ant-design-vue'; |
| | | |
| | | import { requestClient } from '#/api/request'; |
| | | import { FilePreviewModal, useFilePreview } from '#/components/file-preview'; |
| | | |
| | | import { UploadResultStatus } from './typing'; |
| | | import { useUpload, useUploadType } from './use-upload'; |
| | |
| | | } |
| | | } |
| | | |
| | | const { openPreview, previewState } = useFilePreview(); |
| | | const previewLoading = ref(false); |
| | | |
| | | /** 处理文件预览 */ |
| | | function handlePreview(file: UploadFile) { |
| | | async function handlePreview(file: UploadFile) { |
| | | const url = (file as any)?.url || (file as any)?.response?.url || (file as any)?.response; |
| | | if (!url) { |
| | | message.warning('文件地址不存在,无法预览'); |
| | | return; |
| | | } |
| | | const name = file.name || (file as any)?.fileName || ''; |
| | | if (isImage(name || url)) { |
| | | window.open(url, '_blank'); |
| | | return; |
| | | } |
| | | previewLoading.value = true; |
| | | try { |
| | | const blob = await requestClient.get(url, { responseType: 'blob' }) as unknown as Blob; |
| | | const blobUrl = URL.createObjectURL(blob); |
| | | openPreview(blobUrl, name); |
| | | } catch { |
| | | message.error('文件加载失败,请尝试下载后查看'); |
| | | } finally { |
| | | previewLoading.value = false; |
| | | } |
| | | emit('preview', file); |
| | | } |
| | | |
| | |
| | | 格式文件 |
| | | </div> |
| | | </Upload> |
| | | <FilePreviewModal |
| | | v-model:visible="previewState.visible.value" |
| | | :file-url="previewState.fileUrl.value" |
| | | :file-name="previewState.fileName.value" |
| | | /> |
| | | </div> |
| | | </template> |
| | | |