Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't download multiple images from array using axios

const Fs = require('fs')  
const Path = require('path')  
const Axios = require('axios')
var dir = './tmp';


async function downloadImage () {  
if (!Fs.existsSync(dir)){
 Fs.mkdirSync(dir);
}
 var arr = ['https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/01-copy.jpg','https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/02-copy.jpg','https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/03-copy.jpg']
for(var i=0;i<arr.length;i++){
var  url = arr[i]
  
 var name = i  + '.jpg'
 
  var path = Path.resolve(__dirname,dir, name)
  var writer = Fs.createWriteStream(path)

  var response = await Axios({
    url,
    method: 'GET',
    responseType: 'stream'
  })

  response.data.pipe(writer)

  return new Promise((resolve, reject) => {
    writer.on('finish', resolve)
    writer.on('error', reject)
  })
  
}
}
downloadImage()  

This is the above code I am using to download images when I tried downloading multiple images whose links are in array it only downloads the image of first array i.e. arr[0] and can’t figure out what’s the problem it doesn’t give any error to and i can individually download single images but not bulky.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, I played around with this code snippet and I was able to fix by removing the promise callback at the bottom of your for loop. You were right, it would only complete one GET request, and then terminate. It now runs through all three, and saves it in your ./tmp directory.const

Fs = require("fs");
const Path = require("path");
const Axios = require("axios");
var dir = "./tmp";

async function downloadImage() {
  if (!Fs.existsSync(dir)) {
    Fs.mkdirSync(dir);
  }
  var arr = [
    "https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/01-copy.jpg",
    "https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/02-copy.jpg",
    "https://reaperscans.com/wp-content/uploads/WP-manga/data/manga_6295b8da2aa90/5461fc34b58cd174c806625056c6e0dc/03-copy.jpg"
  ];


  for (var i = 0; i < arr.length; i++) {
    var url = arr[i];
    // for debugging
    console.log(arr[i])
    var name = i + ".jpg";
    var path = Path.resolve(__dirname, dir, name);
    var writer = Fs.createWriteStream(path);

    var response = await Axios({
      url,
      method: "GET",
      responseType: "stream"
    });
    // for debugging
    console.log(response)
  }
}
downloadImage();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading