简体   繁体   English

nodejs 从网络下载多个文件

[英]nodejs download multiple files from web

I am trying to download multiple files from a OneDrive folder.我正在尝试从 OneDrive 文件夹下载多个文件。 Below has my code but it will only download the last file and not all of them下面有我的代码,但它只会下载最后一个文件而不是所有文件

for(const f in files)
{
    var fileURL = (files[f]["@microsoft.graph.downloadUrl"]);
    var fileName = (JSON.stringify(files[f].name)).slice(1,-1);

    var request = https.get(fileURL, function(response){
        console.log(fileURL);
        if(response.statusCode == 200){
            var file = fs.createWriteStream(`./temp/${userId}/${fileName}`);
            response.pipe(file);
        }
        request.setTimeout(60000,function(){
            request.destroy();
        });
    });
}

ie the console log would print即控制台日志将打印

FILE_URL1
FILE_URL1
FILE_URL1

rather than而不是

FILE_URL1
FILE_URL2
FILE_URL3

Note that if the console.log(fileURL) is placed before var request https.get... it prints out the 3 file urls.请注意,如果将console.log(fileURL)放在var request https.get...之前var request https.get...它会打印出 3 个文件 url。 I'm not sure if its a problem with the loops or if there is something else.我不确定是循环有问题还是还有其他问题。 I am quite new at javascript so I dont know a lot.我对 javascript 很陌生,所以我知道的不多。

Replace the var with const or let you will see the different resultconst替换var或者你看到不同的结果
Description: What's the difference between using "let" and "var"?描述: 使用“let”和“var”有什么区别?

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

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