简体   繁体   English

载入档案的Javascript

[英]Javascript to load file

I want user upon clicking a button it will load the a txt file from a eg Desktop and display the contents. 我希望用户在单击按钮时将从例如桌面加载一个txt文件并显示其内容。

Can this be done? 能做到吗?

I found a lot of examples for loading a file but not displaying it. 我发现了很多加载文件但不显示文件的示例。

JavaScript in the browser can't access the user's file system - it would be a security issue. 浏览器中的JavaScript无法访问用户的文件系统-这将是一个安全问题。

  1. Consider other client side options like Java Applets or Flash. 考虑其他客户端选项,例如Java Applets或Flash。
  2. In the worst-case, you could upload the file to the server and send it back to the client - but there's probably no usecase that calls for such an expensive hack. 在最坏的情况下,您可以将文件上传到服务器,然后将其发送回客户端-但是可能没有用例需要如此昂贵的黑客攻击。

Additionally, check out this SO question - the File API will allow this. 此外,请查看此SO问题 -File API将允许这样做。

There's an excellent tutorial on html5rocks.com, 'Reading Files in JavaScript using the File APIs', that shows how to use the FileReader interface: 在html5rocks.com上有一个很棒的教程,“使用文件API在JavaScript中读取文件”,其中显示了如何使用FileReader界面:

http://www.html5rocks.com/en/tutorials/file/dndfiles/ http://www.html5rocks.com/zh-CN/tutorials/file/dndfiles/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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