Files
td_official/src/utils/batchUpload.ts
2025-06-17 09:49:15 +08:00

68 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 文件路径utils/BatchUploader.ts
/**
* BatchUploader 批量上传工具类
* 用于将大量数据分批发送到后端,避免单次请求数据过大导致失败。
* 支持设置每批数据大小、批次间延迟、上传函数和完成回调。
*/
/**
* BatchUploader 批量上传工具类(安全简洁版)
* 用于将大量数据分批上传,避免一次性请求数据过大导致失败。
*/
export class BatchUploader<T> {
private dataList: T[];
private chunkSize: number;
private delay: number;
private uploadFunc: (chunk: T[], batchIndex: number, totalBatches: number) => Promise<void>;
private onComplete?: () => void;
constructor(options: {
dataList: T[]; // 统一使用一维数组类型
chunkSize?: number;
delay?: number;
uploadFunc: (chunk: T[], batchIndex: number, totalBatches: number) => Promise<void>;
onComplete?: () => void;
}) {
const { dataList, chunkSize = 8000, delay = 200, uploadFunc, onComplete } = options;
this.dataList = dataList;
this.chunkSize = chunkSize;
this.delay = delay;
this.uploadFunc = uploadFunc;
this.onComplete = onComplete;
}
public async start() {
const total = Math.ceil(this.dataList.length / this.chunkSize);
for (let i = 0; i < total; i++) {
const chunk = this.dataList.slice(i * this.chunkSize, (i + 1) * this.chunkSize);
console.log(`正在上传第 ${i + 1}/${total}chunk 大小: ${chunk.length}`);
await this.uploadFunc(chunk, i + 1, total);
if (this.delay > 0 && i + 1 < total) {
await new Promise((res) => setTimeout(res, this.delay));
}
}
this.onComplete?.();
}
}
//使用示例
// const uploader = new BatchUploader({
// dataList: features,
// chunkSize: 15000,
// delay: 200,
// uploadFunc: async (chunk, batchNum, totalBatch) => {
// await addProjectPilePoint();//异步方法
// },
// onComplete: () => {
// console.log('桩点上传完成');
// }
// });
// await uploader.start();