使用java脚本从文本文件中读取内容 [英] read the content from text file using java script

查看:465
本文介绍了使用java脚本从文本文件中读取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript读取文本文件并在html文件中显示内容。
它工作,因为我已经创建了浏览按钮来选择文本文件,但我想从固定的路径文件中读取内容。在这个程序而不是采取文件路径我想从路径读取文件像 - D:/ new folder / abc.text

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

我使用以下代码。

 <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

您不能,不能在网络浏览器上使用标准,也不能访问本地文件系统。允许浏览器读取用户专门为页面标识的文件,并允许它读取它想要的任何文件之间存在巨大差异。如果没有诉诸机制(会触发安全性的东西),例如ActiveX,Flash,签名的 Java小程序等,你根本无法做到后者。 File API需要一个文件 input 元素作为原因的起点。

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.

这篇关于使用java脚本从文本文件中读取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆