0

I am new to Javascript. I want to open a file from the localsystem only not from server ,when the user clicks on a single item from a list .So, I dont know how to open a file in javascript.so,can any one help me to figure it out ?

1 Answers1

0

Here is an answer from @PaoloMoretti.

See this link:

How to Open Local Disk File With Javascript

Hope this helps,

Tim

CODE:

   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('file-content');
      element.innerHTML = contents;
    }

    document.getElementById('file-input')
      .addEventListener('change', readSingleFile, false);
    <input type="file" id="file-input" />
    <h3>Contents of the file:</h3>
    <pre id="file-content"></pre>
Community
  • 1
  • 1