与 node.js 中的 fs.createWriteStream 关联的事件 [英] event associated with fs.createWriteStream in node.js
问题描述
在写入流时达到 EOF 会触发什么事件?我的代码如下.它是根据 http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream
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
但令人惊讶的是,我的结束"事件从未被触发.当我检查 http://nodejs.org/api/stream.html#stream_event_end 时,我看到了可写流'end' 上没有任何事件
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'
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 事件?
How do I capture the EOF event ?
推荐答案
2013 年 10 月 30 日更新
可读 Steam在底层资源完成写入时发出 close
事件..>
r.on('close', function(){
console.log('request finished downloading file');
});
但是如果你想抓住 fs
完成向光盘写入数据的那一刻,你需要 可写流finish
事件:
But if you want to catch the moment when fs
finished writing data to the disc, you need 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);
});
这篇关于与 node.js 中的 fs.createWriteStream 关联的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!