Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 643
saeed Avatar asked Sep 16 '12 17:09

saeed


1 Answers

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.

like image 68
Charlie Key Avatar answered Oct 02 '22 15:10

Charlie Key