我正在尝试使用request模块保存图像下载。与此
request('http://google.com/images/logos/ps_logo2.png').pipe(fs.createWriteStream('doodle.png'));
效果很好。但是,我希望能够在图像完全下载后执行其他操作。如何为fs.createWriteStream提供回调函数?
您想提前创建流,然后在关闭事件上执行某些操作。
var picStream = fs.createWriteStream('doodle.png');
picStream.on('close', function() {
console.log('file done');
});
request('http://google.com/images/logos/ps_logo2.png').pipe(picStream);
应该这样做。
遇到此错误的人必须尝试在方法fs.createWriteStream(1,2)的第二个输入参数处添加函数,如下例所示,这也适用于fs.unlink方法。请注意,添加的这个新函数什么都不返回,但是它是必需的,如果需要,您还可以添加一些要返回的东西,在我看来不是。
request('http://google.com/images/logos/ps_logo2.png').pipe(fs.createWriteStream('doodle.png'), function(err){});