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

312
Vistas
How can I load text file in javascript using fileReader?

I'm trying to load simple text file in javascript, unfortunately with no success. my code is:

var my_text:any;
var my_file:File = new File([], "C:\\Users\\riki7\\Downloads\\classes.txt");
var reader = new FileReader();
reader.onload = function() {
  my_text = reader.result;
};
reader.readAsText(my_file);
alert(my_text);

after this code runs, I would expect to see classes.txt file content in pop-up alert, instead I get 'undefined'. my file contains a, b, c.

does anyone know what is my problem? maybe the first parameter for File() constructor?

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

0

You have to use html tag <input type="file" id="input" /> and then hung a event listener on it, like that

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}

then after simply bypass your file into the FileReader

const reader = new FileReader();
reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
reader.readAsDataURL(file);

And i guess that would be it. You can find more examples there: https://developer.mozilla.org/en-US/docs/Web/API/File_API/Using_files_from_web_applications

Having your code where your alert runs upfront the callback function. If you need to see alter with the content, simply move your alert into the callback function:

reader.onload = function() {
  my_text = reader.result;
  alert(my_text);
};

because my_text is not ready when you call alert outside.

about 4 years ago · Juan Pablo Isaza Denunciar

0

<html>
    <head>
    <script>
      var fileReadEvent = function(event) {
        var input = event.target;

        var reader = new FileReader();
        reader.onload = function(){
          var text = reader.result;
          alert(text)
        };
      };
    </script>
    </head>
    <body>
    <input type='file' accept='text/plain' onchange='fileReadEvent(event)'><br>
    </body>
    </html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

<input type="file" id="selectedFile">
<p id="display"></p>
<script>
var fr = new FileReader();
let test;
document.getElementById('selectedFile').addEventListener('change', x);

    
function x() {
fr.onload = ()=>{
document.getElementById('display').innerText = fr.result;
test = fr.result;
alert(fr.result);


}
fr.readAsText(this.files[0]);
}   


</script>

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