How to read a local text file?

前端 未结 20 2261
北恋
北恋 2020-11-21 05:28

I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working.

20条回答
  •  -上瘾入骨i
    2020-11-21 06:04

    var input = document.getElementById("myFile");
    var output = document.getElementById("output");
    
    
    input.addEventListener("change", function () {
      if (this.files && this.files[0]) {
        var myFile = this.files[0];
        var reader = new FileReader();
        
        reader.addEventListener('load', function (e) {
          output.textContent = e.target.result;
        });
        
        reader.readAsBinaryString(myFile);
      }   
    });
    
    

提交回复
热议问题