feat: 是否在组件Unmounted时取消上传

This commit is contained in:
dap 2025-03-30 12:59:18 +08:00
parent b9843c6faf
commit f16afe657e
6 changed files with 32 additions and 10 deletions

View File

@ -20,18 +20,21 @@ export interface UploadResult {
* *
* @param file * @param file
* @param otherData * @param otherData
* @param onUploadProgress * @param options
* @param options.onUploadProgress
* @param options.signal
* @returns * @returns
*/ */
export function uploadApi( export function uploadApi(
file: Blob | File, file: Blob | File,
otherData?: Record<string, any>, otherData?: Record<string, any>,
onUploadProgress?: AxiosProgressEvent, options?: { onUploadProgress?: AxiosProgressEvent; signal?: AbortSignal },
) { ) {
const { onUploadProgress, signal } = options ?? {};
return requestClient.upload<UploadResult>( return requestClient.upload<UploadResult>(
'/resource/oss/upload', '/resource/oss/upload',
{ file, ...otherData }, { file, ...otherData },
{ onUploadProgress, timeout: 60_000 }, { onUploadProgress, signal, timeout: 60_000 },
); );
} }

View File

@ -33,6 +33,7 @@ const props = withDefaults(defineProps<FileUploadProps>(), {
preview: defaultFilePreview, preview: defaultFilePreview,
enableDragUpload: false, enableDragUpload: false,
directory: false, directory: false,
abortOnUnmounted: true,
}); });
/** 返回不同的上传组件 */ /** 返回不同的上传组件 */

View File

@ -10,7 +10,7 @@ import type { BaseUploadProps } from './props';
import type { AxiosProgressEvent, UploadResult } from '#/api'; import type { AxiosProgressEvent, UploadResult } from '#/api';
import type { OssFile } from '#/api/system/oss/model'; import type { OssFile } from '#/api/system/oss/model';
import { computed, ref, watch } from 'vue'; import { computed, onUnmounted, ref, watch } from 'vue';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
@ -206,6 +206,7 @@ export function useUpload(
return file; return file;
} }
const uploadAbort = new AbortController();
/** /**
* *
* @param info * @param info
@ -222,11 +223,10 @@ export function useUpload(
const percent = Math.trunc((e.loaded / e.total!) * 100); const percent = Math.trunc((e.loaded / e.total!) * 100);
info.onProgress!({ percent }); info.onProgress!({ percent });
}; };
const res = await api( const res = await api(info.file as File, props?.data ?? {}, {
info.file as File, onUploadProgress: progressEvent,
props?.data ?? {}, signal: uploadAbort.signal,
progressEvent, });
);
info.onSuccess!(res); info.onSuccess!(res);
if (props.showSuccessMsg) { if (props.showSuccessMsg) {
message.success($t('component.upload.uploadSuccess')); message.success($t('component.upload.uploadSuccess'));
@ -237,6 +237,10 @@ export function useUpload(
} }
} }
onUnmounted(() => {
props.abortOnUnmounted && uploadAbort.abort();
});
/** /**
* list地址变化 watch * list地址变化 watch
* immediate用于初始化触发 * immediate用于初始化触发

View File

@ -42,6 +42,7 @@ const props = withDefaults(defineProps<ImageUploadProps>(), {
listType: 'picture-card', listType: 'picture-card',
helpMessage: true, helpMessage: true,
enableDragUpload: false, enableDragUpload: false,
abortOnUnmounted: true,
}); });
// ossId // ossId

View File

@ -89,4 +89,9 @@ export interface BaseUploadProps {
* @param file file * @param file file
*/ */
preview?: (file: UploadFile) => Promise<void> | void; preview?: (file: UploadFile) => Promise<void> | void;
/**
* Unmounted时取消上传
* @default true
*/
abortOnUnmounted?: boolean;
} }

View File

@ -5,7 +5,7 @@ import { h, ref } from 'vue';
import { Page } from '@vben/common-ui'; import { Page } from '@vben/common-ui';
import { Alert, Card, Modal } from 'ant-design-vue'; import { Alert, Card, Modal, Switch } from 'ant-design-vue';
import { FileUpload, ImageUpload } from '#/components/upload'; import { FileUpload, ImageUpload } from '#/components/upload';
@ -27,6 +27,8 @@ function customAccept(accept: string) {
.map((str) => str.toUpperCase()) .map((str) => str.toUpperCase())
.join(','); .join(',');
} }
const showComponent = ref(true);
</script> </script>
<template> <template>
@ -127,6 +129,12 @@ function customAccept(accept: string) {
accept-format="自定义显示允许的文件类型" accept-format="自定义显示允许的文件类型"
/> />
</Card> </Card>
<Card title="默认在unMounted会取消上传" size="small">
<div>将开发者工具调整网络为3G 切换挂载/卸载 可见请求在卸载被取消</div>
挂载/卸载组件: <Switch v-model:checked="showComponent" />
<FileUpload v-if="showComponent" v-model:value="singleFileId" />
</Card>
</div> </div>
</Page> </Page>
</template> </template>