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

241
Visualizações
How can I read a text file with a custom extension using p5js

I want to load text files of a custom extension from disk using p5js, preferably with createFileInput(). I can save a text file of a given custom extension using createWriter(), which I can then access and read with no problems on my default notepad. The problem I'm facing occurs when trying to load said text file using createFileInput(), which leads to a file of .type "" instead of the desired "text". For example,

function setup() {
  load = createFileInput(loadFile)
  noCanvas();
  let content = 'Hello World';
  let writer = createWriter('saveFile.ctxt');
  writer.write([content]);
  writer.close();
}

function loadFile(file) {
  console.log(file.type, file.data);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

Outputs "" data:application/octet-stream;base64,SGVsbG8gV29ybGQ= when loading saveFile.ctxt, which I can't comprehend at all.

Desired output is "text" Hello World, regardless of the custom file extension used.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a FileReader and the FileReader.readAsText() method, taking care to pass file.file from the p5.js callback into it.

file.file is the "underlying File object. All normal File methods can be called on this".

let fileInput;

function setup() {
  noCanvas();
  noLoop();

  // Used to create the file:
  // (probably won't work in Stack Snippet)
  //const writer = createWriter("saveFile.ctxt");
  //writer.write(["Hello World"]);
  //writer.close();

  fileInput = createFileInput(file => {
    const reader = new FileReader();
    reader.onload = e => {
      console.log(e.target.result); // => Hello World
    };
    reader.readAsText(file.file);
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

If you were to pass a .txt file to the input, file.data will have the result text already, without the need for the FileReader.

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