js read text file

Hello geeks! In this post I have shared the details about “js read text file”. Okay, let’s move on to the tutorial.

js read text file

<!-- No need for fetch or ajax.
 Use the widely supported FileReader api. -->
<input type="file" name="inputfile" id="inputfile">
<pre id="output"></pre>
<script type="text/javascript">
	document.getElementById('inputfile')
		.addEventListener('change', function() 

			var fr = new FileReader();
			fr.onload = function() 
				document.getElementById('output')
					.textContent = fr.result;
			

			fr.readAsText(this.files[0]);
		)
</script>
// As with JSON, use the Fetch API & ES6
fetch('something.txt')
  .then(response => response.text())
  .then(data => 
  	// Do something with your data
  	console.log(data);
  );
fetch("file.txt")
	.then((response) => 
  		return response.text();
	)
	.then((text) => 
  		console.log(text);
	);

Final Words

I hope this article helps you to know about “js read text file”. If you have any questions regarding this tutorial please let us know via the comment section. Share this post with your friends & family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment