Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda