Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda