如何使用 JavaScript 访问本地系统中的文件?
我正在使用 jQuery Mobile 框架。我有一台托管网站的服务器。用户可以通过移动浏览器连接到网站并从该网站下载文件(.doc、.xls、.pdf 等)。我需要使用 JavaScript 以编程方式打开保存在用户移动设备中的文件。我尝试使用 location.href="file://sdcard/download/test.doc"
打开。 但这没有用。它显示许可被拒绝。有什么办法吗?请帮忙。提前致谢。
I'm using jQuery Mobile framework. I'm having a server which hosts a website. The user can connect to website through mobile browser and download files (.doc, .xls, .pdf etc.) from that website. I need to open the file which is saved in the user's mobile programmatically using JavaScript. I tried to open using location.href="file://sdcard/download/test.doc"
.
But it didn't work. It showed permission denied. Is there any way to this? Please help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
出于安全原因,浏览器将阻止对本地文件系统的所有访问。您必须使用其他扩展来访问这些文件,或者将它们下载到本地变量而不是文件系统。
如果普通的旧 JavaScript 可以访问您的文件系统,那么坏人就可以更轻松、更高效地做坏事。如果您不使用浏览器,则有几种可能性:
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
http://www.webreference.com/js/column71/
这些文章相当特定于平台,但没有利用最佳安全实践。 买者自负
For security reasons, browsers will block all access to your local file system. You would have to use other extensions to access those files--or download them to a local variable instead of the file system.
If regular old JavaScript could access your filesystem bad people would be able to do really bad things with greater ease and efficiency. There are a couple of possibilities if you are not using a browser:
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
http://www.webreference.com/js/column71/
Those articles are fairly platform specific, and do not leverage the best practices for security though. Caveat Emptor
您无法仅从纯基于 Web 的 JavaScript 访问文件系统,尤其是不能以跨平台或跨浏览器的方式。但是您可以从 Internet Explorer 上的 ActiveX 控件、Firefox 扩展以及 Java 小程序访问文件系统,您可以使用 JavaScript 与所有这些控件进行交互。当然,您必须首先让用户在浏览器上安装 ActiveX、扩展程序或小程序。
You can't access the filesystem from pure web-based JavaScript alone, especially not in a cross-platform or cross-browser manner. But you can access the filesystem from ActiveX Controls on Internet Explorer, from Firefox extensions, and from Java applets, all of which you might be able to interact with using JavaScript. Of course you'll have to get the user to install the ActiveX, extension, or applet on the browser first.
在 Javascript 中没有办法做到这一点。出于安全原因,断然拒绝访问本地文件。
您可以使用
元素链接到该文件,但即使在大多数浏览器中也是禁用的。
There is no way to do this in Javascript. Access to local files is categorically denied for security reasons.
You can link to the file using a
<a>
element but even that is disabled in most browsers.