javscriptHTML Javascript 

How to read the file content from javascript

How to Read Any File Content using javaScript. To Read a file Content in JavaScript just follow the below Script.
HTML:

<input type="file" id="fileinput" />
<h3>file Content:</h3>
<pre id="filecontent"></pre>
html

Javascript:

<script>
function readSingleFile(e) {
  var file = e.target.files[0];
  if (!file) {
    return;
  }
  var reader = new FileReader();
  reader.onload = function(e) {
    var contents = e.target.result;
    displayContents(contents);
  };
  reader.readAsText(file);
}

function displayContents(contents) {
  var element = document.getElementById('filecontent');
  element.innerHTML = contents;
}

document.getElementById('fileinput')
  .addEventListener('change', readSingleFile, false);
</script>
JavaScript

 

Arjun is is a web developer/designer, who is fond of the web. He lives in Manipal, Karnataka, India.

Also Read:  On Scroll Fix the Header to Top HTML/CSS/Javascript

Related posts