saeed
saeed

Reputation: 3923

Callback function after image has downloaded

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 ?

Upvotes: 18

Views: 10192

Answers (2)

Sergio SSantiago
Sergio SSantiago

Reputation: 41

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

Upvotes: 0

Charlie Key
Charlie Key

Reputation: 3440

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.

Upvotes: 49

Related Questions