写入文件文件在Firefox中不起作用 [英] Writing to text file not working in Firefox

查看:68
本文介绍了写入文件文件在Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个文本文件,并以这种方式在我的Firefox扩展中写一些文本:

I am trying to create a text file, and write some text to it within my Firefox extension in this way:

try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}
catch (e) {alert("Permission to write file denied."); return 0;}

var file = Components.classes["@mozilla.org/file/local;1"]
           .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\BrightCloud_Main\test.txt");
file.create(0x00, 0644);
		
var sFileContent = "This test number 1";
outputStream = Components.classes['@mozilla.org/network/file-outputstream;1']
               .createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file,0x04|0x08|0x20,420,0);
outputStream.write(sFileContent, sFileContent.length);
outputStream.flush();
outputStream.close();



它总是触发异常,显示消息并退出。关于如何更改用户权限以允许创建和编辑文件的任何想法?!



提前致谢!


It always fires an exception, displays the message and exits. Any ideas on how to change user privileges to allow to create and edit files?!

Thanks in advance!

推荐答案

大量答案来自Google [ ^ ]。


这是正确的方法:

This is the right way to do it:
var file = Components.classes["@mozilla.org/file/local;1"]
           .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\test.txt");		

// WRITE
var text = "Text to be written";
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                   .createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file, 0x04 | 0x08 | 0x10, 00002, 0);
outputStream.write(text, text.length);
	
// READ
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                  .createInstance(Components.interfaces.nsIFileInputStream);
var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
                  .createInstance(Components.interfaces.nsIScriptableInputStream);
inputStream.init(file, 0x04, 00004, 0);
sstream.init(inputStream);
var output = sstream.read(sstream.available());

alert(output);



有关所有可能的文件标志的列表初始化输入/输出流时可以使用文件权限,访问约书亚的文章


For a list of all the possible file flags and file permissions that can be used when initializing the Input/Output stream, visit JOSHUA''s Article.


这篇关于写入文件文件在Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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