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 -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -