JavaScript/Firefox 写入本地驱动器上的文本文件示例
我知道出于安全考虑,JavaScript 不是为文件操作而设计的。然而,最近我想出了一种使用 Firefox 读取文件的方法。有没有办法也写入文件?
我找到了以下两个潜在的解决方案:
但是,由于我在这方面非常缺乏经验,所以我不知道如何使用其中任何一个他们。有人可以提供一个非常基本的例子吗?
谢谢
编辑:我尝试使用 1. 写入文件,但它在这里工作是我写的:
- WritingJS.html: http://pastebin.com/ZSztcgNx
- selfcontainedmap2.js:http://pastie.org/3391242
我的 Firefox 是 Firefox 8 的全新安装,因此我想应该不会有任何插件冲突。
I understand javascript is not designed for file manipulation due to security concerns. However, recently I figured out a way to read files using firefox. Is there a way to write to a file as well?
I found the following two potential solutions:
- How to enable local javascript to read/write files on my PC?
- https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO
However, since I am extremely inexperienced in this matter I did not figure out how to use either of them. Could anyone provide a very basic example please?
Thanks
Edit: I tried to use 1. to write to a file, but it work here is what I wrote:
- WritingJS.html: http://pastebin.com/ZSztcgNx
- selfcontainedmap2.js: http://pastie.org/3391242
My firefox is a clean install of Firefox 8, hence there shouldn't be any addon conflicts I suppose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法从非特权代码访问 XPCOM 组件。
这在英语中的意思是,这些 API 仅适用于在浏览器上运行的插件等内容,而不适用于网站代码。
除了 cookie 之外,如果没有像 flash 这样的插件提供这样做的方法,网站就没有办法(并且出于安全原因永远不会有办法)读取/写入本地文件。
You won't be able to access the XPCOM components from non-privileged code.
What that means in English is that those APIs are only available to things like addons running on the browser, not on website code.
There is no way (and will never be a way for security reasons) for a website to read/write local files, apart from cookies, without some plugin like flash providing a way of doing it.
您可以将文本写入文件并将其保存在本地驱动器中。一个非常基本的示例如下,
TextArea1 是包含目标文本的表单元素的 id。
该函数可以在按钮单击事件中调用。
希望有帮助!
You can write text to a file and save it in the local drive. A very basic example is as follows,
The TextArea1 is the id of a form element which contains the target text.
The function can be called in a button click event.
Hope that helps!