How to Read Text File in JavaScript

Introduction

In this tutorial we will create a How to Read Text File in JavaScript. This tutorial purpose is to show you how to open and read a text file. This tackle all the needed functionalities that will allow you display the content of the text file. I will provide a sample program to show you the actual coding for this tutorial.

This tutorial is surely easy to create just follow the instruction and you will be good. This program can be use to any part of your on working program if you need to load some text file content. I will do my best to give you the easiest and simplest way of creating this program How to Read Text File. So let's do the coding

What is JavaScript ?

JavaScript is a side scripting language that enables you to create a dynamically content, control multimedia, animating objects, etc. The primary use of JavaScript is to build an interactive web-based applications. It can easily manipulate html and css coding script by accessing its element attribute. Most of the famous website use JavaScript in order to provide stunning and interactive experience such as facebook, youtube, google, etc.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display buttons and a textarea that will upload the text content. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="container-fluid">  
  13.                 <div class="row">
  14.                         <div class="col-md-3"></div>
  15.                         <div class="col-md-6 well">
  16.                                 <h3 class="text-primary">How to Read Text File in JavaScript</h3>
  17.                                 <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18.                                 <textarea id="output" readonly="readonly" style="width:100%; height:300px;"></textarea>
  19.                                 <input type="file" name="inputfile" id="inputfile">
  20.                                 <br />
  21.                                 <button onclick="upload()">Upload</button>
  22.                         </div>
  23.                 </div>
  24.         </div> 
  25. </body>
  26. <script src="js/script.js"></script>
  27. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will load the content of the text file in the textarea after being uploaded. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function upload(){
  2.        
  3.         var file =document.getElementById('inputfile');
  4.         var fr=new FileReader();
  5.         fr.onload=function(){
  6.                 document.getElementById('output').innerHTML=fr.result;
  7.         }
  8.        
  9.         fr.readAsText(file.files[0]);
  10.        
  11. }

In this code we will first create a method that will be called by the html button with the onclick function. We will now bind and target the input file attributes in order to get the details of the uploaded file. Then we use the FileReader() function to read the content of the file.

After reading the text file we will now try to load the content into the textarea using the onload function. This will allow you to load any text file content you uploaded in the textarea.

Output:

The How to Read Text File in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Read Text File in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment