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

365
Vistas
How to create a file with a specific size with NodeJs

Doing several tests of files validation, I need a file with more than 250MB, but I don't want to store it in the repo.

I'm trying to create a file with NodeJs in time execution with several functions I've found, but they don't work for me.

This one creates the file, but it doesn't have the size I'm passing. Files have just 1 byte.

createFile(filePath, size) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    try {
                        const fd = fs.openSync(`${filePath}`, 'w');
                        //fs.writeSync(fd, 'ok', Math.max(0, size - 2));
                        fs.writeSync(fd, Buffer.alloc(1, 'ok', 'base64'), 0, 1,
                            //Math.max(0, size - 1)
                            size - 1
                        );
                        fs.closeSync(fd);
                        resolve(true);
                    } catch (error) {
                        reject(error);
                    }
                }, 0);
            });
        }

I'll appreciate any help

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

0

Buffer.alloc(1...) may be the issue

When I said that, I meant that Buffer.alloc(someNumber) would be the length of your buffer, which is why the length of the file written to ended up as 1

I kept your return promise logic in the createFile function below

createFile(filePath, size) {
  return new Promise((work,fail)=>{
    fs.writeFile(filePath, Buffer.alloc(size), err=>err?fail(err):work())
    //fs.writeFile(desiredPath, desiredSize, (reject if error and resolve if none))
  })
}
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