I am using fetch API in my react app to download a file from a URL. Each URL provides one chunk as the response. When the file has only one chunk, i can use the response.blob() to download the file. However if there are multiple chunks for a single file,i have to fetch the chunks from n different urls, say url1,url2,..so on. How can I combine their responses to generate a single blob and download the file.
Below is the code to download the file with only one chunk(fetched from url-1).
let downloadUrl = `https://url-1`;
fetch(downloadUrl, {
method: 'GET'
})
.then(
(res: any) => {
if (res.ok && res.status === 200) {
//File response loaded
} else {
return null;
}
return res.blob();
},
error => {
// error in downloading
}
)
.then(fileData => {
if (fileData) {
let url = window.URL.createObjectURL(fileData);
let a = document.createElement('a');
a.href = url;
a.download = file.fileName;
a.click();
}
});
I can make multiple requests in the fetch API and use Promise.all(responseArr). But how to combine their responses into single blob response ?