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);
}
})();
You will run this in the browser or Node?
– Paulo GR
in the browser, but if not possible, run on the server
– Elisangela Carla DE Souza