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

170
Vistas
Fetching URLs via JS resulting in empty files

All I want to do is have a nice and simple function that'll take an url and save it to a file, however every single implementation me or some JS developer friends have been able to come up with throughout the day gives the same end result: an empty file.

import * as fs from 'fs';
import * as path from 'path';
import fetch from 'node-fetch';
import { Headers } from 'node-fetch';

export function downloadURL(url, saveLocation) {
    const absSaveLocation = path.resolve(saveLocation);

    const myHeaders = new Headers();
    myHeaders.append('User-Agent', 'hifumi-js:v1.0.0');

    if (url.includes("pximg")) {
      myHeaders.append('Referer', 'https://www.pixiv.net/');
    }

    const requestOptions = {
      method: 'GET',
      headers: myHeaders,
    }

    const fileStream = fs.createWriteStream(absSaveLocation);
    fetch(url, requestOptions)
      .then(res => res.body.pipe(fileStream))
      .catch(error => console.log('error', error));
    fileStream.on('finish', () => {fileStream.close();});
}

I've tried a bunch of different libraries, including https, request, fetch, axios and all of them give me the same useless, empty file. It can't be the url as well since it's matched against a regex first before downloading and I've confirmed with countless different urls.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So I managed to fix this myself in the end by abandonding the whole idea of trying to use a stream to write to the file.

const fileStream = fs.createWriteStream(absSaveLocation);
    fetch(url, requestOptions)
      .then(res => res.body.pipe(fileStream))
      .catch(error => console.log('error', error));
    fileStream.on('finish', () => {fileStream.close();});

has now become

await fetch(url, requestOptions)
      .then(response => response.arrayBuffer())
      .then(buffer => fs.writeFile(absSaveLocation, new Uint8Array(buffer)))
      .catch(error => console.log('error', error));
about 4 years ago · Juan Pablo Isaza 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