Hi,
I have developed something similar except it’s simultaneous download.
I am downloading multiple images in one function call.
Here is code:
function downloadResourcesOffline(data){
var imgsArr = [];
for(var i=0;i<data.length;i++){
var resourceName = data[i].name;
resourceName = resourceName.replace(/-/g, '');
imgsArr.push({url:data[i].url,name:resourceName});
}
var downloadImages = function(callback) {
console.log("images length "+imgsArr.length);
if(imgsArr.length > 0) {
var file = imgsArr.shift();
console.log("processing...");
download_file_httpget('images',file.url,file.name+'.jpg').then(()=>{
downloadImages(callback);
})
} else {
callback();
}
}
downloadImages(function() {
console.log("....Images downloaded....");
});
}
// Function to download file using HTTP.get
function download_file_httpget(folderToStore,file_url,file_name) {
return new Promise(function(resolve,reject){
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
};
var file = fs.createWriteStream(path.join(localResourcesPath,folderToStore,file_name));
http.get(options, function(res) {
res.on('data', function(data) {
file.write(data);
}).on('end', function() {
file.end();
resolve('done');
}).on('error',function(err){
reject(err);
});
});
})
}
Or you can visit this
For some private project I needed to download a huge file from the web with the follwoing constraints : Fast : multi-threaded download seemed to be the go-to solution Error prone : it should handle network errors, eventual app crash, etc. without...