read data from a url and download

Asked

Viewed 675 times

0

hello, need to download various urls, run the code below within 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();

Anyone have any other suggestions? Thanks

  • You will run this in the browser or Node?

  • in the browser, but if not possible, run on the server

1 answer

1


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

npm i request

The following function will perform the download of the file of 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 one array in smaller segments to perform in parallel and execute the call within a repeat loop:

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);
  }
})();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.