与 node.js 中的 fs.createWriteStream 关联的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13156243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
event associated with fs.createWriteStream in node.js
提问by user644745
What event is triggered when EOF is reached while writing to a stream ? My code is as follows. and it is as per http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream
写入流时达到 EOF 时触发什么事件?我的代码如下。它是根据http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream
But surprisingly my 'end' event is never fired. When I checked http://nodejs.org/api/stream.html#stream_event_end, I see that writable stream does not have any event on 'end'
但令人惊讶的是,我的“结束”事件从未被触发。当我检查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);
}
How do I capture the EOF event ?
如何捕获 EOF 事件?
回答by Leonid Beschastny
Updated 30 Oct 2013
2013 年 10 月 30 日更新
Readable Steams emit closeevent when the underlying resource done writing.
当底层资源完成写入时,可读 Steam会发出close事件。
r.on('close', function(){
console.log('request finished downloading file');
});
But if you want to catch the moment when fsfinished writing data to the disc, you need Writeable Stream finishevent:
但是,如果您想捕捉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);
});

