Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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();
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!