I'm trying to implement a file upload in Angular. To avoid file size restrictions, I implemented an API endpoint that receives chunks of a file. After all chunks are received another endpoint should be called that reassembles the chunks on the server. Therefor I need to know when all post requests are finished. Preferably without using Promises. The problem is, I'm new to Angular (and even JS) so I'm wondering how this can work.
let i = 0;
for(let offset = 0; offset < file.size; offset += chunkSize) {
let chunk = file.slice( offset, offset + chunkSize );
let formData = new FormData();
formData.append("fileUpload", chunk, file.name + ".part" + i);
formData.append("identifier", identifier.toString());
this.http.post(this.baseUrl + "Upload", formData).subscribe();
}
You put these into an array and utilize forkJoin and then subscribe to that.
This will call all the http requests at one time, and the subscribe function will be called once all of them are complete.
let i = 0;
const obsArray = [];
for( let offset = 0; offset < file.size; offset += chunkSize ){
let chunk = file.slice( offset, offset + chunkSize );
let formData = new FormData();
formData.append("fileUpload", chunk, file.name + ".part" + i);
formData.append("identifier", identifier.toString());
obsArray.push(
this.http.post(this.baseUrl + "Upload", formData)
).
}
forkJoin(obsArray).subscribe();