简体   繁体   English

JavaScript读取文件内容

[英]JavaScript read file contents

how can you retrieve the data from a document with javascript that isn't the page you are on if you have the url of the new document. 如果具有新文档的url,如何使用javascript(不是您所在的页面)从文档中检索数据。
what i am trying to do is create a page that has a text field for providing a local file name and a button that retrieves the words from the document provided. 我正在尝试做的是创建一个页面,该页面具有用于提供本地文件名的文本字段和用于从提供的文档中检索单词的按钮。

thanks. 谢谢。

HTML5 has a File API that lets you read local files. HTML5具有File API,可让您读取本地文件。 It's supported in at least Firefox (3.6 and later, I think) and Chrome. 至少Firefox(我认为是3.6和更高版本)和Chrome支持该功能。 I don't know if any other browsers support it yet or not. 我不知道是否有其他浏览器支持它。 If you need to support other browsers, you'll have to fall back to something like Flash, but I don't have any experience with that. 如果您需要支持其他浏览器,则必须使用Flash之类的东西,但是我对此没有任何经验。

Unfortunately, by default Chrome doesn't allow local files to access other local files (each file is considered to be from its own domain). 不幸的是,默认情况下,Chrome不允许本地文件访问其他本地文件(每个文件都被视为来自其自己的域)。 You can explicitly allow it by adding the --allow-file-access-from-files flag when you launch Chrome. 您可以在启动Chrome时添加--allow-file-access-from-files标志,以明确允许它。

Here's a good introduction to the File API with several examples: http://www.html5rocks.com/en/tutorials/file/dndfiles/ . 下面通过几个示例很好地介绍了File API: http : //www.html5rocks.com/en/tutorials/file/dndfiles/

Browser security does not allow direct access to the local filesystem. 浏览器安全性不允许直接访问本地文件系统。 If it could, web pages would be able to steal any file of your machine. 如果可以的话,网页将能够窃取您计算机中的任何文件。

HTML5 local storage does allow local access, but on a different principle. HTML5本地存储确实允许本地访问,但是基于不同的原理。

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

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