我试图通过Observables和递归请求一个接一个地上传一堆文件(一个请求一个到另一个),但是它消耗非常大的RAM内存,甚至还完成了不释放内存的操作。
我的示例代码(仅包含核心逻辑方法,而不包含完整的组件)
PersonInfo界面:
$1$2 (or in some regex flavors: \1\2)
PersonInfo组件代码:
interface PersonInfo {
personId: number;
name: string:
email: string;
resume: File;
progress: number;
isUpload: boolean;
}
在Init块上:
uploadPersonInfo() {
const uploadCollection: Array<PersonInfo> = [
{ personId: 1, name: "Ram", email: "ram@example.com", resume: fileCollection[0], progress: 0, isUpload: false },
{ personId: 2, name: "Kumar", email: "kumar@example.com", resume: fileCollection[1], progress: 0, isUpload: false },
{ personId: 3, name: "Prakash", email: "prakash@example.com", resume: fileCollection[2], progress: 0, isUpload: false },
];
const httpUploadPersonInfo = (person: PersonInfo): Observable<HttpEvent<boolean>> => {
const payload = new FormData();
const httpOption = {
headers: new HttpHeaders(),
reportProgress: true
};
payload('id', person.personId);
payload('name', person.name);
payload('email', person.email);
payload('resume', person.resume, person.resume.name);
const req = new HttpRequest('POST', 'http://localhost:3000/Person/UploadPersonInfo', payLoad, httpOptions);
return this.http.request<boolean>(req);
}
let index = 0;
const upload = (): any => {
if (index === uploadCollection.length) {
return;
}
const person: PersonInfo = uploadCollection[index];
httpUploadPersonInfo().subscribe((event: HttpEvent<boolean>) => {
switch (event.type) {
case HttpEventType.UploadProgress:
person.progress = Math.round((event.loaded / person.resume.size) * 100)
break;
case HttpEventType.Response:
if(event && event.body == true) {
index += 1;
upload();
}
break;
}
});
}
upload();
}
RAM消耗:
注意:我探讨了以下问题,但已完全解决了我的问题 需求 Angular 2 Http, Observables and recursive requests
我需要按顺序依次上传所有人员信息 不平行。而且我需要监视文件的进度 也上传。
请告诉我优化的方法,该方法可以通过上载文件进度选项按顺序执行此操作。