22 Feb 2021

How to Read text file using JavaScript of Web Browsers?


In this post, I am going to read a .txt file using JavaScript. HTML5 does provide File & FileReader API. By which we are going to read text file.
FileReader API has following methods to read a file.

FileReader.readAsArrayBuffer()
Read file's Content in the form of Array.

FileReader.readAsBinaryString()
This read file content as binary String. Eg -1001000100

FileReader.readAsDataURL()
This read as data URL. Like image data Uri.

FileReader.readAsText()
Reads file raw content as default defined UTF-8 text format.

<input type="file" id="infile" /> 
<script>

infile.onchange = function (e){ 

var filereader = new FileReader();

filereader.readAsTextFile(this.file[0]); 

filereader.onload = function (){

}

} 
</script>

Online Preview


Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE