I am working with the following code snippet:
getFileAsync(fieldFiles: Array<FileFields>): Observable<Array<UploadFile>> {
const files = Array<UploadFile>();
const downloads = Array<Observable<any>>();
fieldFiles.forEach(file => {
downloads.push(FileHelper.downloadFile(file.file_id));
});
forkJoin(downloads).subscribe({
next: filesData => {
console.log(filesData)
fieldFiles.forEach((file, index) => {
const fileData = filesData[index];
files.push({
uid: file.file_id,
name: file.filename,
url: fileData ? window.URL.createObjectURL(new Blob([fileData.data], {type: file.contentType})) : require('../../../assets/images/file.png'),
size: file.size,
type: file.contentType,
})
})
},
error: () => {}
});
}
Essentially, I have a list of files that I want to download one by one:
I am using FileHelper.downloadFile
which returns an HTTP observable.
const downloads = Array<Observable<any>>();
fieldFiles.forEach(file => {
downloads.push(FileHelper.downloadFile(file.file_id));
});
Afterwards, I use forkJoin(downloads) to make the request and receive the responses.
However, I am interested in using concatMap
instead to download the files sequentially.
The challenge is that the HTTP request does not return the file ID, so I need a way to track which file ID is currently being downloaded. For example:
const downloads = Array<Observable<any>>();
fieldFiles.forEach(file => {
const fileId = file.file_id;
downloads.push(FileHelper.downloadFile(fileId));
});
concatMap(downloads).subscribe({
next: (filesData, fileId) => {}
Is there a method to include this variable and associate it with the observable of the HTTP request?