如何在Mozilla Firefox中使用JavaScript创建文件 [英] How to create a file using javascript in Mozilla Firefox

查看:180
本文介绍了如何在Mozilla Firefox中使用JavaScript创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在javascript中编写一个函数来创建一个文件,然后写一些内容,iam和firefox一起工作,任何人都可以帮我解决这个问题。谢谢...

解决方案

您可以在Firefox中使用JavaScript编写文件,但必须使用XPCOM对象(内部浏览器API )。这是不允许从网页加载的JavaScript,它是打算在Firefox加载项(具有高级别的权限)内运行的JavaScript使用。



非特权(网页)JavaScript有一种方法可以请求更多特权,如果用户授予它(会弹出一个对话框询问权限),网页代码将能够写入文件。但是在你进一步阅读之前,有一个警告:

这不是标准的JavaScript,我不会推荐这种方法你正在开发一个非常具体的应用程序,这将被用于一个非常具体的方式(例如, http:// www .tiddlywiki.com / 一个客户端JavaScript-HTML唯一wiki)。

在网站上申请XPCOM权限是一个不好的做法!这基本等同于运行刚刚从站点下载的.exe文件。您要求用户授予对计算机的完全访问权限(读取,写入,执行)与运行Firefox的用户的身份。



请求使用XPCOM的权限(this将提示用户确认,没办法避免它):

pre $ netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect) ;然后,使用XPCOM对象(来自Mozilla Developer Network的示例代码)写入文件: $ / code>

p>

  1. //文件是nsIFile,数据是字符串
2. var foStream = Components.classes [@ mozilla .ORG /网络/文件输出流; 1\" ]。
3. createInstance(Components.interfaces.nsIFileOutputStream);
4.
5. //使用0x02 | 0x10打开文件进行追加。
6. foStream.init(文件,0x02 | 0x08 | 0x20,0666,0);
7. //写入,创建,截断
8. //在ac文件操作中,我们不需要使用或者操作来设置文件模式
9. //直接使用r 或w通常。
10.
11. //如果你确定在数据中永远不会有任何非ASCII文本,你可以
12. //也可以直接调用foStream.writeData
13. var converter = Components.classes [@ mozilla.org/intl/converter-output-stream; 1]。
14. createInstance(Components.interfaces.nsIConverterOutputStream);
15. converter.init(foStream,UTF-8,0,0);
16. converter.writeString(data);
17. converter.close(); //关闭foStream

您可以在这里使用XPCOM在Firefox中找到更多关于I / O的信息: a href =https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O =nofollow noreferrer> https://developer.mozilla.org/zh-CN/docs/Code_snippets/File_I_O


I want to write a function in javascript which creates a file and write some content to it, iam working with firefox, can anybody help me in this case.

Thanks...

解决方案

You can write files in JavaScript in Firefox, but you have to use an XPCOM object (internal browser API). This is not allowed for JavaScript loaded from a web page, and it is intended to be used by JavaScript running inside a Firefox add-on (with high level of privileges).

There is a way for unprivileged (web page) JavaScript to request more privileges and if the user grants it (there will be a pop up dialog asking for permission), the web page code would be able to write to a file.

But before you read further, a warning:

This is not standard JavaScript and I would not recommend this approach unless you are developing a very specific application, that will be used in a very specific way (like for example, http://www.tiddlywiki.com/ a client-side JavaScript-HTML only wiki).

Requesting XPCOM privileges on a website is a bad practice! It's basicly equivalent to running an .exe you just downloaded from a site. You are asking a user to grant full access to their computer (read, write, execute) with the identity of the user running Firefox.

Request permission to use XPCOM (this will prompt the user for confirmation, no way to avoid it):

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

Then, write to a file using an XPCOM object (example code from Mozilla Developer Network):

   1. // file is nsIFile, data is a string  
   2. var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].  
   3.                          createInstance(Components.interfaces.nsIFileOutputStream);  
   4.   
   5. // use 0x02 | 0x10 to open file for appending.  
   6. foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);   
   7. // write, create, truncate  
   8. // In a c file operation, we have no need to set file mode with or operation,  
   9. // directly using "r" or "w" usually.  
  10.   
  11. // if you are sure there will never ever be any non-ascii text in data you can   
  12. // also call foStream.writeData directly  
  13. var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].  
  14.                           createInstance(Components.interfaces.nsIConverterOutputStream);  
  15. converter.init(foStream, "UTF-8", 0, 0);  
  16. converter.writeString(data);  
  17. converter.close(); // this closes foStream  

You can find more information about I/O in Firefox using XPCOM here: https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O

这篇关于如何在Mozilla Firefox中使用JavaScript创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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