18

I'm trying to save an image download with the request module. With this

request('http://google.com/images/logos/ps_logo2.png').pipe(fs.createWriteStream('doodle.png')); 

It works fine. But I want to be able to do something else after the image has been completely downloaded. How can provide a callback function to fs.createWriteStream ?

saeed
  • 3,861
  • 3
  • 25
  • 23

2 Answers2

49

You want to create the stream ahead of time and then do something on the close event.

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); 

This should do it.

Charlie Key
  • 3,440
  • 1
  • 21
  • 12
0

People getting this error must try adding a function at the second input parameter of method fs.createWriteStream(1,2) as the example below , this also works for fs.unlink method. Note that this new function added returns nothing but it is neccesary, you can also add something to return if needed, not in my case.

request('http://google.com/images/logos/ps_logo2.png').pipe(fs.createWriteStream('doodle.png'), function(err){});