小编典典

与node.js中的fs.createWriteStream关联的事件

node.js

写入流时达到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事件?


阅读 359

收藏
2020-07-07

共1个答案

小编典典

2013年10月30日更新

当基础资源完成写入时,可读Steam
会发出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);
});
2020-07-07