對於那些可能處於這個位置的人,我提出的解決方案利用了我現有的代碼,只需要添加JSZip即可。代碼爲:
public downloadAllFiles() {
console.log("Downloading All Files");
var fileName = this.defaultDataService.defaultDataBase + "-" + this.selectedYear.path + ".zip";
var zip = new JSZip();
var year = zip.folder(this.selectedYear.path);
for (var i = 0; i < this.selectedTypes.length; i++) {
var subDir = year.folder(this.selectedTypes[i].path);
for (var j = 0; j < this.selectedTypes[i].pdfList.length; j++) {
subDir.file(this.selectedTypes[i].pdfList[j].name + ".pdf", this.createPdfBlob(this.convertBase64ToBytes(this.selectedTypes[i].pdfList[j].bytes)));
}
}
zip.generateAsync({ type: "blob" })
.then(function (blob) {
FileSaver.saveAs(blob, fileName);
});
}
convertBase64ToBytes(data: string) {
let byteCharacters = atob(data);
let byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
let byteArray = new Uint8Array(byteNumbers);
return byteArray;
}
createPdfBlob(src: any) {
const byteArrays = [];
byteArrays.push(src);
return new Blob(byteArrays, { type: 'application/pdf' });
}