与node.js中的fs.createWriteStream关联的事件
在写入stream时遇到EOF时会触发什么事件? 我的代码如下。 它是根据http://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream
但令人惊讶的是,我的“终结”事件从未被解雇。 当我检查http://nodejs.org/api/stream.html#stream_event_end ,我看到可写入的stream没有任何事件'结束'
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事件?
2013年10月30日更新
当底层资源完成写入时,可读的蒸汽发出close
事件 。
r.on('close', function(){ console.log('request finished downloading file'); });
但是,如果您想抓住fs
完成将数据写入光盘的时刻,则需要Writeable Stream finish
事件 :
var w = fs.createWriteStream('/tmp/imageresize/'+x); request(options1).pipe(w); w.on('finish', function(){ console.log('file downloaded to ', '/tmp/imageresize/'+x); });