read data from a url and download

0

Hello, I need to download several urls, I execute the code below inside a for, but only works for the last url.

var anchor = document.createElement('a');
anchor.href = 'http://e-gov.betha.com.br/e-nota/teste';
anchor.download = 'http://e-gov.betha.com.br/e-nota/teste';
document.body.appendChild(anchor);
anchor.click();

Does anyone have any other suggestions? Thank you

    
asked by anonymous 08.10.2018 / 17:01

1 answer

1

To download a file using Node.js you can use a http request module and use stream response to generate a file. In my example I'll use the module request . To install it use the command:

npm i request

The following function will perform the download of the file from a URL to the destination folder:

const { createWriteStream } = require('fs');
const request = require('request');

const baixar = (url) => {
  return new Promise((resolver) => {
    const partes = url.split('/');
    const arquivo = createWriteStream(partes[partes.length - 1]);

    request(url).on('response', (resposta) => {
      const stream = resposta.pipe(arquivo);

      stream.on('finish', resolver);
    });
  });
};

To download multiple files you can create a function that divides a array into smaller segments to perform in parallel and execute the call within a loop repetition:

const dividir = (origem, tamanho) => {
  const segmentos = [];

  for (let i = 0; i < origem.length; i += tamanho) {
    segmentos.push(origem.slice(i, i + tamanho));
  }

  return segmentos;
};

(async () => {
  const links = [
    "http://localhost/arquivos/arquivo1.png",
    "http://localhost/arquivos/arquivo2.png",
    "http://localhost/arquivos/arquivo3.png",
    "http://localhost/arquivos/arquivo4.png",
    "http://localhost/arquivos/arquivo5.png",
    "http://localhost/arquivos/arquivo6.png",
    "http://localhost/arquivos/arquivo7.png",
    "http://localhost/arquivos/arquivo8.png",
  ];

  const partes = dividir(links, 3);

  for (parte of partes) {
    const promessas = parte.map(link => baixar(link));

    await Promise.all(promessas);
  }
})();
    
16.11.2018 / 17:17