下面是调用下面函数的代码片段。这是一个允许人们将文件从系统拖放到网站的过程。它显示所有文件的列表,并在加载时带有进度条。它在大多数情况下都可以正常工作,但是当有大量文件时,我会遇到一些问题。我有一个正在加载的测试目录,其中包含 100 多个文件。正在加载的第一个文件非常小,因此似乎在设置可观察对象之前已加载它们,因为进度条未显示任何进度并且 forkJoin 未完成,但如果我查看系统文件是实际加载。
我没有正确设置主题吗?有没有更好的方法来跟踪正在上传的文件的进度?任何帮助,将不胜感激。
if (this.files.size > 0) {
this.progress = await this.uploadService.dndUpload(
this.files, this.currentDir, this.currentProject, timestamp
);
let allProgressObservables = [];
for (let key in this.progress) {
allProgressObservables.push(this.progress[key].progress);
}
this.sfUploadSnackbar.openSnackbar(this.files, this.progress);
forkJoin(allProgressObservables).subscribe(async end => {
this.sfUploadSnackbar.closeSnackbar();
this.uploadService.clearUploadDir(this.currentProject, timestamp)
.subscribe();
this.uploadInProgress = false;
this.getFiles();
});
}
async dndUpload(files: Set<any>, dir: string, projectId: number, timestamp: number) {
const status: { [key: string]: { progress: Observable<number> } } = {};
for (let it = files.values(), file = null; file = it.next().value;) {
let params = new HttpParams()
.set('dir', dir)
.set('path', file.fullPath.replace(file.name,''))
.set('projectId', projectId.toString())
.set('timestamp', timestamp.toString());
let f: File = await new Promise((resolve, reject) => file.file(resolve, reject))
const formData: FormData = new FormData();
formData.append('file', f, f.name);
const req = new HttpRequest('POST', '/api/dndUpload', formData, {
reportProgress: true, params
});
const progress = new Subject<number>();
status[file.name] = {
progress: progress.asObservable()
};
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * event.loaded / event.total);
progress.next(percentDone);
} else if (event instanceof HttpResponse) {
progress.complete();
}
});
}
return status;
}