feat: 文件上传 进度条+提示文字

This commit is contained in:
dap 2025-01-12 11:57:09 +08:00
parent 2378728d7f
commit 684cc83256

View File

@ -2,6 +2,10 @@
import type { UploadFile, UploadProps } from 'ant-design-vue'; import type { UploadFile, UploadProps } from 'ant-design-vue';
import type { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface'; import type { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface';
import type { AxiosResponse } from '@vben/request';
import type { AxiosProgressEvent } from '#/api';
import { ref, toRefs, watch } from 'vue'; import { ref, toRefs, watch } from 'vue';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
@ -25,7 +29,10 @@ const props = withDefaults(
* 包括拓展名(不带点) 文件头(image/png等 不包括泛写法即image/*) * 包括拓展名(不带点) 文件头(image/png等 不包括泛写法即image/*)
*/ */
accept?: string[]; accept?: string[];
api?: (...args: any[]) => Promise<any>; api?: (
file: Blob | File,
onUploadProgress?: AxiosProgressEvent,
) => Promise<AxiosResponse<any>>;
disabled?: boolean; disabled?: boolean;
helpText?: string; helpText?: string;
// eslint-disable-next-line no-use-before-define // eslint-disable-next-line no-use-before-define
@ -39,6 +46,10 @@ const props = withDefaults(
// support xxx.xxx.xx // support xxx.xxx.xx
// url // url
resultField?: 'fileName' | 'ossId' | 'url'; resultField?: 'fileName' | 'ossId' | 'url';
/**
* 是否显示下面的描述
*/
showDescription?: boolean;
value?: string | string[]; value?: string | string[];
}>(), }>(),
{ {
@ -52,6 +63,7 @@ const props = withDefaults(
multiple: false, multiple: false,
api: uploadApi, api: uploadApi,
resultField: 'url', resultField: 'url',
showDescription: true,
}, },
); );
const emit = defineEmits(['change', 'update:value', 'delete']); const emit = defineEmits(['change', 'update:value', 'delete']);
@ -204,12 +216,19 @@ async function customRequest(info: UploadRequestOption<any>) {
return; return;
} }
try { try {
const res = await api?.(info.file); //
const progressEvent: AxiosProgressEvent = (e) => {
const percent = Math.trunc((e.loaded / e.total!) * 100);
info.onProgress!({ percent });
};
const res = await api?.(info.file as File, progressEvent);
/** /**
* 由getValue处理 传对象过去 * 由getValue处理 传对象过去
* 直接传string(id)会被转为Number * 直接传string(id)会被转为Number
* 内部的逻辑由requestClient.upload处理 这里不用判断业务状态码 不符合会自动reject
*/ */
info.onSuccess!(res); info.onSuccess!(res);
message.success($t('component.upload.uploadSuccess'));
// //
const value = getValue(); const value = getValue();
isInnerOperate.value = true; isInnerOperate.value = true;
@ -264,6 +283,7 @@ function getValue() {
:list-type="listType" :list-type="listType"
:max-count="maxNumber" :max-count="maxNumber"
:multiple="multiple" :multiple="multiple"
:progress="{ showInfo: true }"
@preview="handlePreview" @preview="handlePreview"
@remove="handleRemove" @remove="handleRemove"
> >
@ -272,6 +292,16 @@ function getValue() {
<div style="margin-top: 8px">{{ $t('component.upload.upload') }}</div> <div style="margin-top: 8px">{{ $t('component.upload.upload') }}</div>
</div> </div>
</Upload> </Upload>
<div
v-if="showDescription"
class="mt-2 flex flex-wrap items-center text-[14px]"
>
请上传不超过
<div class="text-primary mx-1 font-bold">{{ maxSize }}MB</div>
<div class="text-primary mx-1 font-bold">{{ accept.join('/') }}</div>
格式文件
</div>
<Modal <Modal
:footer="null" :footer="null"
:open="previewOpen" :open="previewOpen"