Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

366
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda