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 ?

3
  • 5
    You can found an answere Here. Commented Mar 25, 2016 at 8:30
  • I have tried that already. But its not working... Commented Mar 25, 2016 at 8:34
  • Did you check browser compatibility ? Commented Mar 25, 2016 at 8:36

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Please do not copy answers from other questions. This question should be marked as a duplicate instead.
Ok, no problem. How do I do that?
Once you have more reputation, you can vote to mark questions as duplicate. For now, just leave a comment like Jean-Charbel VANNIER did.