简体   繁体   中英

read the content from text file using java script

I am trying to read a text file using javascript and display the content in html file. Its working as i have created the browse button to select the text file but i want to read the content from a fixed path file .In this program instead of taking the file path i want to read file from the path like - D:/new folder/abc.text

I am using the following code.

 <html>
 <input type="file" id="fileinput"/>
 <script type="text/javascript">
 function readSingleFile(evt) {
 var f = evt.target.files[0]; 
 if (f) {
 var r = new FileReader();
 r.onload = function(e) { 
 var contents = e.target.result;
 document.write("the contents of the file are<br>");
 document.write(contents);
  }
   r.readAsText(f);
  } else { 
   alert("Failed to load file");
  }
 }
  document.getElementById('fileinput').addEventListener('change',readSingleFile,false);
  </script>
   </html>

...but i want to read the content from a fixed path

You can't, not on a web browser, using standards, and accessing the local file system. There's a huge difference between allowing the browser to read a file the user has specifically identified for the page, and allowing it to read any file it wants. You simply cannot do the latter without resorting mechanisms (which will trigger security stuff) such as ActiveX, Flash, signed Java applets, and the like. The File API requires a file input element as a starting point for a reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM