1

Is it possible to open a local file from a local page using FileReader without the <input type=file id=files> The file is in the same directory as the page on a local machine.

reader = new FileReader();
reader.readAsText("output_log.txt");
user873477
  • 87
  • 2
  • 8
  • Possible duplicate of [Javascript - read local text file](http://stackoverflow.com/questions/14446447/javascript-read-local-text-file) – PM 77-1 Jan 28 '16 at 03:49
  • Possible duplicate of [HTML5 File API: How to see the result of readAsText()](http://stackoverflow.com/questions/13729301/html5-file-api-how-to-see-the-result-of-readastext) – Kevin Hernandez Jan 28 '16 at 04:41

1 Answers1

0

I have created a sample code using Jquery/javascript which will read a local text file and display its content in Html page

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){

$("#fileinput").on('change',function(evt){
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;

        $("#filename").html(f.name);
        $("#fileType").html(f.type);
        $("#display_file_content").html(contents);
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    } 

});

});

</script>
</head>

<body>
<label>Please upLoad a file</label>
<input type="file" id="fileinput" />
</br>
<div>
<b>File Name:-</b><label id="filename"></label> </br>
<b>File Type:-</b><label id="fileType"></label></br>
<b>Content<b/>
<pre id="display_file_content">
</pre>
<div>
</body>
</html>