簡體   English   中英

在繼續下一次迭代之前,NodeJS用管道完成了文件的寫入

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

與這個問題類似,

我有一個腳本,可以通過http.get將文件下載到給定的URL。

在僅使用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;   
}

是否有一種更清潔的方法類似於我上面所想到的? 還是我應該采取不同的方式? 我試圖將代碼放在.on('end'但它什么也沒做

end事件不是在請求上觸發的,而是在響應( docs )上觸發的:

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

正如@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM