在写入流时达到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);
}
Run Code Online (Sandbox Code Playgroud)
如何捕获EOF事件?
Leo*_*tny 70
当底层资源完成写入时,可读Steams 会发出close事件.
r.on('close', function(){
console.log('request finished downloading file');
});
Run Code Online (Sandbox Code Playgroud)
但是如果你想抓住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);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25682 次 |
| 最近记录: |