Site icon GetCodify

How to read the file content from javascript

javscript

javscript

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

 

Exit mobile version