27

ストリームへの書き込み中に EOF に達すると、どのイベントがトリガーされますか? 私のコードは次のとおりです。http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-streamのとおりです

しかし、驚くべきことに、私の「終了」イベントは決して発生しません。http://nodejs.org/api/stream.html#stream_event_endを確認すると、書き込み可能なストリームには「終了」にイベントがないことがわかります


var x = a1.jpg;
var options1 = {'url': url_of_an_image, 'encoding': null};
var r = request(options1).pipe(fs.createWriteStream('/tmp/imageresize/'+x));

r.on('end', function(){
    console.log('file downloaded to ', '/tmp/imageresize/'+x);
}

EOF イベントをキャプチャするにはどうすればよいですか?

4

2 に答える 2

84

2013 年 10 月 30 日更新

読み取り可能な Steamは、基になるリソースが書き込みを完了するとイベントを発行closeします。

r.on('close', function(){
  console.log('request finished downloading file');
});

fsただし、ディスクへのデータの書き込みが終了した瞬間を捉えたい場合は、 Writeable Streamfinishイベントが必要です。

var w = fs.createWriteStream('/tmp/imageresize/'+x);

request(options1).pipe(w);

w.on('finish', function(){
  console.log('file downloaded to ', '/tmp/imageresize/'+x);
});
于 2012-10-31T11:51:17.850 に答える