简体   繁体   English

在继续下一次迭代之前,NodeJS用管道完成了文件的写入

[英]NodeJS finish writing the file with pipe before continuing with the next iteration

Similar to this question , 与这个问题类似,

I have a script that downloads a file to a given url via http.get. 我有一个脚本,可以通过http.get将文件下载到给定的URL。

How can I make sure the pipe is finished before continuing to the next iteration with just the http/https module?? 在仅使用http/https模块继续下一次迭代之前,如何确保pipe已完成?

    //nodejs default libs
    var fs = require("fs"); 
    var http = require('https');

    function dlFile(fullFilePath, dlUrl, fsize, fname){
        var file = fs.createWriteStream(fullFilePath); //fullFilePath will dictate where we will save the file + filename.
        var rsult ='';
        var downloadedFsize;
        var stats; //stats of the file will be included here

        var request = http.get( dlUrl, function(response) {
                let rsult = response.statusCode;
                //will respond with a 200 if the file is present
                //404 if file is missing 
                response.pipe(file);

                /*pipe writes the file... 
                  how do we stop the iteration while it is not yet finished writing?
                */

                console.log(" \n FILE  : " + fname);
                console.log("File analysis finished : statusCode: " +  rsult + " || Saved on " +  fullFilePath);
                console.log(' \n Downloaded from :' + dlUrl);
                console.log(' \n SQL File size is : ' + fsize);
                //identify filesize 
                stats = fs.statSync(fullFilePath);
                downloadedFsize = stats["size"]; //0 because the pipe isn't finished yet...

                console.log(' actual file size is : ' + downloadedFsize);
            }).on('error', function(e) {
                console.error(e);
                //log that an error happened to the file
            }).on('end', function(e){
                //tried putting the above script here but nothing happens
            });
        return rsult;   
}

Is there a cleaner approach similar to what I have in mind above? 是否有一种更清洁的方法类似于我上面所想到的? or should I approach this differently? 还是我应该采取不同的方式? I tried putting the code on .on('end' but it does nothing 我试图将代码放在.on('end'但它什么也没做

The end event is not triggered on the request, instead it is triggered on the response ( docs ): end事件不是在请求上触发的,而是在响应( docs )上触发的:

 response.on("end", function() {
   console.log("done");
 });

As @Jonas Wilms says, the trigger was indeed on response. 正如@Jonas Wilms所说,触发确实在响应上。

//nodejs default libs
    var fs = require("fs"); 
    var http = require('https');

    function dlFile(fullFilePath, dlUrl, fsize, fname){
        var file = fs.createWriteStream(fullFilePath); //fullFilePath will dictate where we will save the file + filename.
        var rsult ='';
        var downloadedFsize;
        var stats; //stats of the file will be included here

        var request = http.get( dlUrl, function(response) {
                let rsult = response.statusCode;
                //will respond with a 200 if the file is present
                //404 if file is missing 
                response.pipe(file).on('finish', function(e){
                   console.log(" \n FILE  : " + fname);
                   console.log("File analysis finished : statusCode: " +  rsult + " || Saved on " +  fullFilePath);
                   console.log(' \n Downloaded from :' + dlUrl);
                   console.log(' \n SQL File size is : ' + fsize);
                   //identify filesize 
                   stats = fs.statSync(fullFilePath);
                   downloadedFsize = stats["size"]; 
                   console.log(' actual file size is : ' + downloadedFsize);
                });

                /*pipe writes the file above, and output the results once it's done */


            }).on('error', function(e) {
                console.error(e);
                //log that an error happened to the file
            }).on('end', function(e){
                //tried putting the above script here but nothing happens
            });
        return rsult;   
}

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

相关问题 在继续之前等待节点完成从 https.get 响应写入文件? - Wait for node to finish writing file from https.get response before continuing? NodeJS Promise.all在等待所有组件完成之后再继续 - NodeJS Promise.all is waiting for all components to finish before continuing NodeJS Plotly - 如何在继续之前等待 getImage 完成 - NodeJS Plotly - how to wait for getImage to finish before continuing 如何在继续下一行代码之前等待 .map() 完成执行 - How to wait for .map() to finish executing before continuing on to next lines of code 等待函数完成,然后再继续执行$ .each函数中的下一个循环 - wait for the function to finish before continuing to the next loop in a $.each function 如何在继续执行脚本之前有效地等待链接文件完成运行 - How to efficiently wait for a linked file to finish running before continuing with script Javascript:等待循环中的函数在下一次迭代之前完成执行 - Javascript: wait for function in loop to finish executing before next iteration NodeJS - 如何在继续执行流程之前等待多个异步调用完成? - NodeJS - How to wait for multiple Async calls to finish before continuing execution flow? 节点 - 在继续之前等待地图完成 - Node - wait for map to finish before continuing 如何继续等待一个功能完成? - How to wait for one function to finish before continuing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM