2

using the request package https://github.com/request/request

as in the docs says:

You can stream any response to a file stream.

var stream = request('https://images-na.ssl-images-amazon.com/images/I/31TsfgL0mzL._AC_SY200_.jpg').pipe(fs.createWriteStream('test1.png'))

trying to download a file/image the file "test1.png" is created but is empty the size is 0kb. The variable stream is WriteStream do I need to invoke the write method or do some other step?

H.C
  • 565
  • 7
  • 28
  • as @Srishti sayed and this post seems that being in function helped, also as in my script is fetching multiple images it seems it takes some time to write the stream to the files. – H.C Oct 25 '18 at 15:17
  • This question is asking what could go wrong when downloading an image and streaming a response to a file using the Nodejs `request` module`. I think it's wrongfully marked as a duplicate of the generic question [Downloading images with node.js](https://stackoverflow.com/questions/12740659/downloading-images-with-node-js) that by the way is closed because too broad. – user2314737 Jan 05 '19 at 14:25
  • Also, the `request` module is now deprecated – Washington Guedes May 27 '20 at 13:34

1 Answers1

2

Wrapping the download request in a function worked for me!

var stream = function(){
        request('https://images-na.ssl-images-amazon.com/images/I/31TsfgL0mzL._AC_SY200_.jpg').pipe(fs.createWriteStream('test1.png'));
    }
stream();

The image (5.9 kB) was downloaded and saved.

Srishti
  • 396
  • 1
  • 9