我正在尝试在另一个.subscribe((中的.subscribe((中执行操作:收集文件,以便在此之后都可以打开它们。
我正在使用jszip来处理代码的" zipping"部分。
我已经尝试使用.map((而不是通过for循环进行迭代。我在这里阅读(在订阅中致电订阅是好方法吗?(我可以使用.flatmap((解决问题,因为我的第二个可观察的(下载文件(取决于第一个结果(获取文件列表(,但我仍然无法弄清楚...
我目前有一个正在工作的实现,但是它使用settimeout((,所以它"等待"了要下载文件,因此我可以将它们汇编,但我相信那不是最好的方法。
this.downloadingZipFile = true;
let zip = new JSZip();
let tableTemplateCollection = '';
let givenName = this.patientGivenName;
let templateIndex = zipTemplate.templateIndex;
console.log('TOTAL FILES COUNT: ' + this.totalFiles);
this.fileViewerDataService.getFileNamesList(this.patient.getPatientQueryParams(), this.totalFiles, 0)
.subscribe((data) => {
let dataLength = Object.keys(data).length;
for (let i = 0; i < dataLength; i++) {
console.log('THIS IS THE ID: ' + data[i]['indexId']);
this.fileViewerDataService.getFileViewByIdAsBlob(data[i]['indexId']).subscribe(patientFile => {
console.log(`Saving... ${data[i]['indexId']}`);
if ((data[i]['name']).includes('immunization')) {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('immunization').file(`${data[i]['name']}.html`, patientFile);
tableTemplateCollection += this.indexCreation(data[i]['name']);
} else if ((data[i]['name']).includes('laboratory')) {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('laboratory').file(`${data[i]['name']}.html`, patientFile);
tableTemplateCollection += this.indexCreation(data[i]['name']);
} else {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('medication').file(`${data[i]['name']}.html`, patientFile);
tableTemplateCollection += this.indexCreation(data[i]['name']);
}
this.downloadingZipFile = false;
});
}
setTimeout(function () {
templateIndex = templateIndex.replace('##NAME##', givenName);
tableTemplateCollection = templateIndex.replace('##FILES##', tableTemplateCollection);
zip.file('index.html', tableTemplateCollection);
zip.file('data.js', translateEnFr.data);
zip.generateAsync({ type: "blob" }).then(function (patientFolders) {
saveAs(patientFolders, "Report.zip");
});
}, 2500);
},
err => {
window.alert(`${err.status}: Error downloading zip from API.`);
})
}
您肯定是正确的,setTimeout()
不是最好的方法。您可以使用这样的承诺:
this.fileViewerDataService.getFileNamesList(this.patient.getPatientQueryParams(), this.totalFiles, 0)
.subscribe((data) => {
let dataLength = Object.keys(data).length;
let promises = [];
for (let i = 0; i < dataLength; i++) {
console.log('THIS IS THE ID: ' + data[i]['indexId']);
promises.push(createPromise(data[i]));
}
Promise.all(promises).then(() => {
templateIndex = templateIndex.replace('##NAME##', givenName);
tableTemplateCollection = templateIndex.replace('##FILES##', tableTemplateCollection);
zip.file('index.html', tableTemplateCollection);
zip.file('data.js', translateEnFr.data);
zip.generateAsync({ type: "blob" }).then((patientFolders) => saveAs(patientFolders, "Report.zip"));
}).catch(err => {
window.alert(`${ err.status }: Error downloading zip from API.`);
});
},
err => {
window.alert(`${ err.status }: Error downloading zip from API.`);
});
createPromise()
看起来像这样:
createPromise(data) {
return new Promise((resolve, reject) => {
this.fileViewerDataService.getFileViewByIdAsBlob(data['indexId']).subscribe(patientFile => {
console.log(`Saving... ${ data['indexId'] }`);
if ((data['name']).includes('immunization')) {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('immunization').file(`${ data['name'] }.html`, patientFile);
tableTemplateCollection += this.indexCreation(data['name']);
} else if ((data['name']).includes('laboratory')) {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('laboratory').file(`${ data['name'] }.html`, patientFile);
tableTemplateCollection += this.indexCreation(data['name']);
} else {
console.log('THIS IS THE PATIENT FILE: ' + patientFile);
zip.folder('medication').file(`${ data['name'] }.html`, patientFile);
tableTemplateCollection += this.indexCreation(data['name']);
}
this.downloadingZipFile = false;
resolve();
}, error => reject(error));
});
}