在 Mozilla Firefox 扩展中写入文件 - 奇怪的错误

发布于 2024-11-04 03:14:04 字数 1780 浏览 2 评论 0原文

我正在尝试开发一个 Firefox 扩展。 刚开始时,我尝试写入文件,但我发现了这种非常奇怪且无法解释的行为。

此代码有效:

var file = showFilePicker(window,"saveTestCaseAs",
                                  Components.interfaces.nsIFilePicker.modeSave,
                                  Format.TEST_CASE_DIRECTORY_PREF,
                                 function(fp) {return fp.file;});

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);

var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);

converter.init(foStream, "UTF-8", 0, 0);

converter.writeString("test string");

converter.close();

而此代码无效:

var file1 = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file1.initWithPath("C:\Documents and Settings\XPMUser\Desktop\test.t"); 

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(foStream, "UTF-8", 0, 0);
converter.writeString("test string");
converter.close(); // this closes foStream

错误消息是: 错误:组件返回失败代码:0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init] 但是,此错误消息没有任何意义,因为这是所有错误中最常见的错误。 这很奇怪,因为唯一的区别是文件对象的初始化方式,但两种方式的初始化参数(文件名、prems 等)和返回对象的类型完全相同。

如果有人对此提供一些线索,我将非常感激。

I'm trying to develop a firefox extension.
Just for beginning I'm trying to write in file but I get this very strange and inexplicable behaviour to me.

this code works:

var file = showFilePicker(window,"saveTestCaseAs",
                                  Components.interfaces.nsIFilePicker.modeSave,
                                  Format.TEST_CASE_DIRECTORY_PREF,
                                 function(fp) {return fp.file;});

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);

var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);

converter.init(foStream, "UTF-8", 0, 0);

converter.writeString("test string");

converter.close();

and this one doesn't:

var file1 = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file1.initWithPath("C:\Documents and Settings\XPMUser\Desktop\test.t"); 

var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
// use 0x02 | 0x10 to open file for appending.
foStream.init(file1, 0x02 | 0x08 | 0x20, 438, 0);
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(foStream, "UTF-8", 0, 0);
converter.writeString("test string");
converter.close(); // this closes foStream

Error message is:
Error: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init]
However this error message doesn't mean anything since this is the most generic error of all.
this is very strange because the only difference is the way the file object is inilialized, but in both way initilization parameters(file name, prems, etc.) and returned object's type are exactly the same.

Will be very gratefull if anyone gives some clue about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-11-11 03:14:04

@sdwilsh 上面的观点非常好。
不幸的是,问题要简单得多,而且愚蠢得多。它是在单个反斜杠和过于笼统的错误消息处。
我提供的路径带有单个反斜杠,它们被解释为转义符号,实际上该字符串不再是有效路径。将 '\' 替换为 '\' 可以解决该问题。
所以这是一个非常简单的问题,但人们应该更加注意细节。

@sdwilsh made very good point above.
Unforunataley the problem is far more simple and event silly. And it is at the single backslashes and the too general error messages.
The path I provided is with single backslashes wich are interpreted as escape symbols and actually the string is no more a valid path. Replacing '\' with '\' solves that problem.
So it's very simple problem but one should really pay more attention to details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文