3

I want to download an image file with nodeJS, by using an API, but the problem is the API link doesn't have .jpg file in the end, how do I do, below is how I am trying

url = 'https://i.pravatar.cc/225'
const https = require('https')
const fs = require('fs');


result = https.get(url, (resp) => {

        console.log('Result of response: ', resp)
        fs.writeFileSync('apiResponse', resp)
        console.log('Reached end!')
    })

When I click the URL it shows the image in browser, how do make my program to write the file on hard-drive,

Lint
  • 895
  • 3
  • 13
  • 32
  • There are many tutorials available for it. Ask if you face issue there – Piyush Oct 31 '19 at 11:33
  • You should search before posting here, find the solution here: https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries – Arun Saini Oct 31 '19 at 11:36
  • 1
    Does this answer your question? [How to download a file with Node.js (without using third-party libraries)?](https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries) – Arun Saini Oct 31 '19 at 11:37
  • I already searched before posting the question, but I was having problem in that – Lint Oct 31 '19 at 11:38

4 Answers4

3

Just pipe response to file

const url = 'https://i.pravatar.cc/225'

const https = require('https')
const fs = require('fs');

https.get(url, resp => resp.pipe(fs.createWriteStream('./test.jpeg')));
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
  • 1
    Worked like magic, how did it worked ?, can you please explain a little – Lint Oct 31 '19 at 11:36
  • socket is stream, file is stream. https://nodejs.org/api/stream.html – Yaroslav Gaponov Oct 31 '19 at 11:40
  • I want to download the files in bulk quantity, using the same code, I tried to put the above snippet in a for loop but it always downloads the last file, what would be the problem ? I think it's related to async wait Can you guide me about that please – Lint Nov 14 '19 at 07:19
3

This code uploads several different pictures

const url = 'https://i.pravatar.cc/225'

const https = require('https')
const fs = require('fs');

for(let i=0; i<10; i++) 
  https.get(url, resp => resp.pipe(fs.createWriteStream(`./test_${i}.jpeg`)));
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
1

please use this I have try with it and working fine you can rename the downloded file too.

const https = require("https");
const fs = require("fs");

const file = fs.createWriteStream("file.jpg");
const request = https.get("https://i.pravatar.cc/225", function(response) {
  response.pipe(file);
});
Pardeep
  • 2,153
  • 1
  • 17
  • 37
  • alright, I've a slight different question, is there any way I can write the `resp` in a text file or any other format, I tried but it was only showing [object object] but when i console logged it, it was a huge object – Lint Oct 31 '19 at 11:40
-3

Try download-file library

https://www.npmjs.com/package/download-file

Install : npm install download-file --save

var download = require('download-file')

var url = "http://i.imgur.com/G9bDaPH.jpg"

var options = {
    directory: "./images/cats/",
    filename: "cat.gif"
}

download(url, options, function(err){
    if (err) throw err
    console.log("meow")
}) 
ArUn
  • 1,317
  • 2
  • 23
  • 39