I am trying to download a number of PDF files from list (var urls) which contains the URLs. The download itself works but the resutlting PDF files are broken, meaning I can save them in my folder but I cannot open them. Is there anyone who has an idea? Here is the code:
var urls=require('./paperURLs.json');
DOWNLOAD_DIR = './paper/';
function readFile(callback) {
if(urls.length > 0) {
var setFile = urls.shift(),
file_name = url.parse(setFile).pathname.split('/').pop(),
trial = setFile.split('/').pop(),
file = fs.createWriteStream(DOWNLOAD_DIR + trial);
http.get(setFile, function(res){
res.on('error', function(err){
console.log(err);
});
res.on('data', function(data){
file.write(data);
console.log(setFile + ' started');
});
res.on('end', function(){
console.log(setFile + ' completed, moving on');
readFile(callback);
});
});
}
}
readFile();
Thanks in advance for your help.
One issue is that you're not closing the file, which could cause some problems. Also, a better/easier way to do this might be to just pipe the streams together instead of doing it manually:
http.get(setFile, function(res) {
res.on('error', function(err) {
console.log(err);
});
res.pipe(file).on('close', function() {
console.log(setFile + ' completed, moving on');
readFile(callback);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.