简体   繁体   中英

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.

How can I make sure the pipe is finished before continuing to the next iteration with just the http/https module??

    //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

The end event is not triggered on the request, instead it is triggered on the response ( docs ):

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

As @Jonas Wilms says, the trigger was indeed on response.

//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;   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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