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

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

View File

@@ -10,7 +10,7 @@ import type { BaseUploadProps } from './props';
import type { AxiosProgressEvent, UploadResult } from '#/api';
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';
@@ -206,6 +206,7 @@ export function useUpload(
return file;
}
const uploadAbort = new AbortController();
/**
* 自定义上传实现
* @param info
@@ -222,11 +223,10 @@ export function useUpload(
const percent = Math.trunc((e.loaded / e.total!) * 100);
info.onProgress!({ percent });
};
const res = await api(
info.file as File,
props?.data ?? {},
progressEvent,
);
const res = await api(info.file as File, props?.data ?? {}, {
onUploadProgress: progressEvent,
signal: uploadAbort.signal,
});
info.onSuccess!(res);
if (props.showSuccessMsg) {
message.success($t('component.upload.uploadSuccess'));
@@ -237,6 +237,10 @@ export function useUpload(
}
}
onUnmounted(() => {
props.abortOnUnmounted && uploadAbort.abort();
});
/**
* 这里默认只监听list地址变化 即重新赋值才会触发watch
* immediate用于初始化触发

View File

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

View File

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