Last active
December 12, 2023 03:02
-
-
Save dmarcosl/e57768283c9b81978987fa366b37d63c to your computer and use it in GitHub Desktop.
Download several files from Firebase Storage through an Angular8 application in zip format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dependencies | |
// angular 8 | |
// "firebase": "^7.6.1", | |
// "file-saver": "^2.0.2", | |
// "jszip": "^3.2.2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FileDownloadEntity { | |
name: string; | |
fileName: string; | |
extension: string; | |
constructor(name: string, fileName: string, extension: string) { | |
this.name = name; | |
this.fileName = fileName; | |
this.extension = extension; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {AngularFireStorage} from '@angular/fire/storage'; | |
import {HttpClient} from '@angular/common/http'; | |
import {saveAs} from 'file-saver/dist/FileSaver'; | |
import {FileDownloadEntity} from 'file-download.entity'; | |
import * as JSZip from 'jszip'; | |
@Injectable() | |
export class StorageService { | |
constructor(private angularFireStorage: AngularFireStorage, | |
private httpClient: HttpClient) { | |
} | |
public downloadZippedFiles(files: FileDownloadEntity[], zipName: string): void { | |
const zipFile: JSZip = new JSZip(); | |
let count = 0; | |
files.forEach(file => { | |
this.angularFireStorage.ref('/' + file.name).getDownloadURL().subscribe(url => { | |
this.httpClient.get(url, {responseType: 'blob'}).subscribe(response => { | |
zipFile.file(file.fileName + file.extension, response, {binary: true}); | |
count++; | |
if (count === files.length) { | |
zipFile.generateAsync({type: 'blob'}).then(content => { | |
saveAs(content, zipName + '.zip'); | |
}); | |
} | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment