javascript - Downloading PDFs by using node.js results in broken files -


i trying download number of pdf files list (var urls) contains urls. download works resutlting pdf files broken, meaning can save them in folder cannot open them. there has idea? here 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 help.

one issue you're not closing file, could cause problems. also, better/easier way might pipe streams instead of doing 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);   }); }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -