简体   繁体   English

使用node.js下载PDF会导致文件损坏

[英]Downloading PDFs by using node.js results in broken files

I am trying to download a number of PDF files from list (var urls) which contains the URLs. 我正在尝试从包含URL的列表(可变URL)中下载许多PDF文件。 The download itself works but the resutlting PDF files are broken, meaning I can save them in my folder but I cannot open them. 下载本身可以正常工作,但是重新输入的PDF文件已损坏,这意味着我可以将它们保存在文件夹中,但无法打开它们。 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);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM