ストリームへの書き込み中にEOFに達したときにどのイベントがトリガーされますか?私のコードは次のとおりです http://docs.nodejitsu.com/articles/ advanced/streams/how-to-use-fs-create-write-stream
しかし、驚いたことに、私の「終了」イベントは発生しません。 http://nodejs.org/api/stream.html#stream_event_end をチェックしたところ、書き込み可能なストリームの「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イベントをキャプチャするにはどうすればよいですか?
読み取り可能なSteam 基になるリソースが書き込みを完了したときにclose
イベントを発行 。
r.on('close', function(){
console.log('request finished downloading file');
});
ただし、fs
がディスクへのデータの書き込みを完了した瞬間をキャッチするには、 Writeable Stream finish
event が必要です。
var w = fs.createWriteStream('/tmp/imageresize/'+x);
request(options1).pipe(w);
w.on('finish', function(){
console.log('file downloaded to ', '/tmp/imageresize/'+x);
});