上传文件支持进度条 参数getPercent

This commit is contained in:
wangwenchen 2025-08-28 10:55:12 +08:00
parent c79b7ca63f
commit c7ebbf00f6
13 changed files with 31 additions and 22 deletions

View File

@ -20,6 +20,6 @@ export declare class ExtraFile<ED extends EntityDict> extends Feature {
} | undefined; } | undefined;
getFileName(extraFile: ED['extraFile']['OpSchema']): string; getFileName(extraFile: ED['extraFile']['OpSchema']): string;
formatBytes(size: number): string; formatBytes(size: number): string;
autoUpload(extraFile: ED['extraFile']['OpSchema'], file: File | string, style?: string): Promise<string>; autoUpload(extraFile: ED['extraFile']['OpSchema'], file: File | string, style?: string, getPercent?: Function): Promise<string>;
private uploadToAspect; private uploadToAspect;
} }

View File

@ -146,7 +146,7 @@ export class ExtraFile extends Feature {
formatBytes(size) { formatBytes(size) {
return bytesToSize(size); return bytesToSize(size);
} }
async autoUpload(extraFile, file, style) { async autoUpload(extraFile, file, style, getPercent) {
const extraFileId = extraFile.id || generateNewId(); const extraFileId = extraFile.id || generateNewId();
const applicationId = extraFile.applicationId || this.application.getApplicationId(); const applicationId = extraFile.applicationId || this.application.getApplicationId();
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
@ -166,7 +166,7 @@ export class ExtraFile extends Feature {
const up = new Upload(); const up = new Upload();
try { try {
const cos = getCos(newExtraFile.origin); const cos = getCos(newExtraFile.origin);
await cos.upload(newExtraFile, up.uploadFile, file, this.uploadToAspect.bind(this)); await cos.upload(newExtraFile, up.uploadFile, file, this.uploadToAspect.bind(this), getPercent);
if (!cos.autoInform()) { if (!cos.autoInform()) {
await this.cache.exec('operate', { await this.cache.exec('operate', {
entity: 'extraFile', entity: 'extraFile',

2
es/types/Cos.d.ts vendored
View File

@ -22,7 +22,7 @@ export interface Cos<ED extends EntityDict> {
autoInform?: boolean) => Promise<any>, file: string | File, uploadToAspect: (file: File | string, name: string, // 文件的part name autoInform?: boolean) => Promise<any>, file: string | File, uploadToAspect: (file: File | string, name: string, // 文件的part name
aspectName: string, // 上传的aspect名 aspectName: string, // 上传的aspect名
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean) => Promise<any>) => Promise<void>; autoInform?: boolean) => Promise<any>, getPercent?: Function) => Promise<void>;
/** /**
* 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径 * 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径
*/ */

View File

@ -13,6 +13,7 @@ export default class CTYun implements Cos<EntityDict> {
upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name
uploadUrl: string, // 上传的url uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean) => Promise<any>, file: string | File): Promise<void>; autoInform?: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
getPercent?: Function) => Promise<any>, file: string | File, _uploadToAspect?: any, getPercent?: Function): Promise<void>;
composeFileUrl(application: Partial<EntityDict['application']['Schema']>, extraFile: Partial<EntityDict['extraFile']['OpSchema']>, style?: string): string; composeFileUrl(application: Partial<EntityDict['application']['Schema']>, extraFile: Partial<EntityDict['extraFile']['OpSchema']>, style?: string): string;
} }

View File

@ -24,7 +24,7 @@ export default class CTYun {
assert(objectId); assert(objectId);
return `extraFile/${objectId}${extension ? '.' + extension : ''}`; return `extraFile/${objectId}${extension ? '.' + extension : ''}`;
} }
async upload(extraFile, uploadFn, file) { async upload(extraFile, uploadFn, file, _uploadToAspect, getPercent) {
const uploadMeta = extraFile.uploadMeta; const uploadMeta = extraFile.uploadMeta;
let response; let response;
try { try {
@ -33,7 +33,7 @@ export default class CTYun {
Policy: uploadMeta.policy, Policy: uploadMeta.policy,
AWSAccessKeyId: uploadMeta.accessKey, AWSAccessKeyId: uploadMeta.accessKey,
signature: uploadMeta.signature, signature: uploadMeta.signature,
}, true); }, true, getPercent);
} }
catch (err) { catch (err) {
// 网络错误 // 网络错误

View File

@ -20,6 +20,6 @@ export declare class ExtraFile<ED extends EntityDict> extends Feature {
} | undefined; } | undefined;
getFileName(extraFile: ED['extraFile']['OpSchema']): string; getFileName(extraFile: ED['extraFile']['OpSchema']): string;
formatBytes(size: number): string; formatBytes(size: number): string;
autoUpload(extraFile: ED['extraFile']['OpSchema'], file: File | string, style?: string): Promise<string>; autoUpload(extraFile: ED['extraFile']['OpSchema'], file: File | string, style?: string, getPercent?: Function): Promise<string>;
private uploadToAspect; private uploadToAspect;
} }

View File

@ -149,7 +149,7 @@ class ExtraFile extends Feature_1.Feature {
formatBytes(size) { formatBytes(size) {
return (0, extraFile_1.bytesToSize)(size); return (0, extraFile_1.bytesToSize)(size);
} }
async autoUpload(extraFile, file, style) { async autoUpload(extraFile, file, style, getPercent) {
const extraFileId = extraFile.id || (0, uuid_1.generateNewId)(); const extraFileId = extraFile.id || (0, uuid_1.generateNewId)();
const applicationId = extraFile.applicationId || this.application.getApplicationId(); const applicationId = extraFile.applicationId || this.application.getApplicationId();
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
@ -169,7 +169,7 @@ class ExtraFile extends Feature_1.Feature {
const up = new upload_1.Upload(); const up = new upload_1.Upload();
try { try {
const cos = (0, index_frontend_1.getCos)(newExtraFile.origin); const cos = (0, index_frontend_1.getCos)(newExtraFile.origin);
await cos.upload(newExtraFile, up.uploadFile, file, this.uploadToAspect.bind(this)); await cos.upload(newExtraFile, up.uploadFile, file, this.uploadToAspect.bind(this), getPercent);
if (!cos.autoInform()) { if (!cos.autoInform()) {
await this.cache.exec('operate', { await this.cache.exec('operate', {
entity: 'extraFile', entity: 'extraFile',

2
lib/types/Cos.d.ts vendored
View File

@ -22,7 +22,7 @@ export interface Cos<ED extends EntityDict> {
autoInform?: boolean) => Promise<any>, file: string | File, uploadToAspect: (file: File | string, name: string, // 文件的part name autoInform?: boolean) => Promise<any>, file: string | File, uploadToAspect: (file: File | string, name: string, // 文件的part name
aspectName: string, // 上传的aspect名 aspectName: string, // 上传的aspect名
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean) => Promise<any>) => Promise<void>; autoInform?: boolean) => Promise<any>, getPercent?: Function) => Promise<void>;
/** /**
* 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径 * 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径
*/ */

View File

@ -13,6 +13,7 @@ export default class CTYun implements Cos<EntityDict> {
upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name upload(extraFile: OpSchema, uploadFn: (file: File | string, name: string, // 文件的part name
uploadUrl: string, // 上传的url uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean) => Promise<any>, file: string | File): Promise<void>; autoInform?: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
getPercent?: Function) => Promise<any>, file: string | File, _uploadToAspect?: any, getPercent?: Function): Promise<void>;
composeFileUrl(application: Partial<EntityDict['application']['Schema']>, extraFile: Partial<EntityDict['extraFile']['OpSchema']>, style?: string): string; composeFileUrl(application: Partial<EntityDict['application']['Schema']>, extraFile: Partial<EntityDict['extraFile']['OpSchema']>, style?: string): string;
} }

View File

@ -26,7 +26,7 @@ class CTYun {
(0, assert_1.assert)(objectId); (0, assert_1.assert)(objectId);
return `extraFile/${objectId}${extension ? '.' + extension : ''}`; return `extraFile/${objectId}${extension ? '.' + extension : ''}`;
} }
async upload(extraFile, uploadFn, file) { async upload(extraFile, uploadFn, file, _uploadToAspect, getPercent) {
const uploadMeta = extraFile.uploadMeta; const uploadMeta = extraFile.uploadMeta;
let response; let response;
try { try {
@ -35,7 +35,7 @@ class CTYun {
Policy: uploadMeta.policy, Policy: uploadMeta.policy,
AWSAccessKeyId: uploadMeta.accessKey, AWSAccessKeyId: uploadMeta.accessKey,
signature: uploadMeta.signature, signature: uploadMeta.signature,
}, true); }, true, getPercent);
} }
catch (err) { catch (err) {
// 网络错误 // 网络错误

View File

@ -84,7 +84,7 @@ export class ExtraFile<ED extends EntityDict> extends Feature {
}, },
}); });
modiEntityId = modi.entityId!; modiEntityId = modi.entityId!;
return modi.data as ED['extraFile']['OpSchema']; return modi.data as ED['extraFile']['OpSchema'];
}; };
const extraFile = getExtraFileData(); const extraFile = getExtraFileData();
@ -197,7 +197,8 @@ export class ExtraFile<ED extends EntityDict> extends Feature {
async autoUpload( async autoUpload(
extraFile: ED['extraFile']['OpSchema'], extraFile: ED['extraFile']['OpSchema'],
file: File | string, file: File | string,
style?: string style?: string,
getPercent?: Function,
) { ) {
const extraFileId = extraFile.id || generateNewId(); const extraFileId = extraFile.id || generateNewId();
const applicationId = extraFile.applicationId || this.application.getApplicationId(); const applicationId = extraFile.applicationId || this.application.getApplicationId();
@ -222,7 +223,8 @@ export class ExtraFile<ED extends EntityDict> extends Feature {
newExtraFile as OpSchema, newExtraFile as OpSchema,
up.uploadFile, up.uploadFile,
file, file,
this.uploadToAspect.bind(this) this.uploadToAspect.bind(this),
getPercent,
); );
if (!cos.autoInform()) { if (!cos.autoInform()) {
await this.cache.exec('operate', { await this.cache.exec('operate', {

View File

@ -37,6 +37,7 @@ export interface Cos<ED extends EntityDict> {
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean // 上传成功是否会自动通知server若不会则需要前台显式通知 autoInform?: boolean // 上传成功是否会自动通知server若不会则需要前台显式通知
) => Promise<any>, ) => Promise<any>,
getPercent?: Function,
) => Promise<void>; ) => Promise<void>;
/** /**
@ -51,7 +52,7 @@ export interface Cos<ED extends EntityDict> {
export interface CosBackend<ED extends EntityDict> { export interface CosBackend<ED extends EntityDict> {
name: string; name: string;
/** /**
* extrafile生成之前token等信息生成并存放在uploadMeta属性中 * extrafile生成之前token等信息生成并存放在uploadMeta属性中
* @param application applciation * @param application applciation
@ -64,7 +65,7 @@ export interface CosBackend<ED extends EntityDict> {
extraFile: ED['extraFile']['OpSchema'], extraFile: ED['extraFile']['OpSchema'],
) => Promise<void>; ) => Promise<void>;
/** /**
* 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径 * 访urlurl应当和objectId一对一映射extraFile之间共享cos上的路径
*/ */

View File

@ -45,9 +45,12 @@ export default class CTYun implements Cos<EntityDict> {
name: string, // 文件的part name name: string, // 文件的part name
uploadUrl: string, // 上传的url uploadUrl: string, // 上传的url
formData: Record<string, any>, // 上传的其它part参数 formData: Record<string, any>, // 上传的其它part参数
autoInform?: boolean // 上传成功是否会自动通知server若不会则需要前台显式通知 autoInform?: boolean, // 上传成功是否会自动通知server若不会则需要前台显式通知
getPercent?: Function,
) => Promise<any>, ) => Promise<any>,
file: string | File file: string | File,
_uploadToAspect?: any,
getPercent?: Function,
) { ) {
const uploadMeta = extraFile.uploadMeta! as CTYunUploadInfo; const uploadMeta = extraFile.uploadMeta! as CTYunUploadInfo;
let response; let response;
@ -62,7 +65,8 @@ export default class CTYun implements Cos<EntityDict> {
AWSAccessKeyId: uploadMeta.accessKey, AWSAccessKeyId: uploadMeta.accessKey,
signature: uploadMeta.signature, signature: uploadMeta.signature,
}, },
true true,
getPercent,
); );
} catch (err) { } catch (err) {
// 网络错误 // 网络错误