使用JavaScript HTML5文件API(离线网站)读取本地文件 [英] Read a local file using JavaScript HTML5 file api (offline website)

查看:155
本文介绍了使用JavaScript HTML5文件API(离线网站)读取本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将打包在.exe文件中的网站。所以该网站只能在离线状态下使用。现在我需要解析一个本地的XML文档。编辑:我不想使用<输入...我怎样才能获得文件句柄到本地文件使用html5文件API? > 或将文件拖入浏览器中。

解决方案

恐怕我可能是您的设计的坏消息:您所要求的操作明确违反了File API规范中指定的安全模型。 FileReader()的客户端实现必须确保FileReader对象正在读取的所有文件首先被用户选中。 (W3C File API,13。安全注意事项: http://www.w3.org/TR/FileAPI/#安全讨论)。



浏览器脚本可能会在没有任何用户交互的情况下从路径中任意打开和读取任何文件,这将是一个巨大的安全风险。没有浏览器制造商允许不受限制地访问整个文件系统。



如果您的脚本确实需要该XML文件,那么您将拥有指导用户如何授予浏览器访问权限,因为每个浏览器都会阻止您的代码在没有用户操作的情况下直接打开它。


I am working on an web site which will be packed in an .exe file. So the site will only be used offline. Now i need to parse an local xml document. How can i get the file handle to a local file using html5 file api?

EDIT: I dont want to use <input...> or dragging file into browser.

解决方案

I'm afraid I may be the bearer of bad news for your design: The action you are requesting expressly violates the security model as specified in the File API spec. The client implementation of FileReader() must make sure that "all files that are being read by FileReader objects have first been selected by the user." (W3C File API , 13. Security Considerations: http://www.w3.org/TR/FileAPI/#security-discussion).

It would be a huge security risk of browser scripts could just arbitrarily open and read any file from a path without any user interaction. No browser manufacturer would allow unfettered access to the entire file system like that.

If your script really needs that XML file, you are going to have to instruct users on how to grant the browser access to it, as every browser will prevent your code from opening it directly without user action.

这篇关于使用JavaScript HTML5文件API(离线网站)读取本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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