Firefox 4 扩展 file.createUnique 异常:NS_ERROR_FAILURE
我开发了一个 Firefox 扩展,它收集一些数据并将它们保存到桌面上的文件中。
它在 Mac OS 上运行良好,但在 Windows 7 上,创建文件时会抛出错误。 例外:
uncaught exception:[Exception... "Component returned failure code:0x80004005(NS_ERROR_FAILURE)[nsIFile.createUnique]" nsresult:"0x80004005 (NS_ERROR_FAILURE)" location: "JS frame::chrome://klmlogger/content/record.js :: <TOP_LEVEL> :: line 59" data: no]
代码在这里:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("Desk", Components.interfaces.nsIFile);
file.append("samplefile.txt");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666); // Line 59
如果我删除第 59 行(并且它仍然可以在 Mac OS 上运行),则错误变为:
Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init]
resource://gre/modules/FileUtils.jsm Line: 86
错误发生在这里:
if (modeFlags === undefined)
modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
fos.init(file, modeFlags, this.PERMS_FILE, 0); // Line 86
return fos;
I developed a firefox extension which collects some data and saves them into a file on desktop.
It works well on Mac OS, however on Windows 7, an error throws when the file is being created.
The exception:
uncaught exception:[Exception... "Component returned failure code:0x80004005(NS_ERROR_FAILURE)[nsIFile.createUnique]" nsresult:"0x80004005 (NS_ERROR_FAILURE)" location: "JS frame::chrome://klmlogger/content/record.js :: <TOP_LEVEL> :: line 59" data: no]
Code is here:
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("Desk", Components.interfaces.nsIFile);
file.append("samplefile.txt");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666); // Line 59
If I remove line 59 (and it still works on Mac OS), the error became:
Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init]
resource://gre/modules/FileUtils.jsm Line: 86
The error occurs here:
if (modeFlags === undefined)
modeFlags = this.MODE_WRONLY | this.MODE_CREATE | this.MODE_TRUNCATE;
fos.init(file, modeFlags, this.PERMS_FILE, 0); // Line 86
return fos;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码对我来说工作正常,这里没有问题。 Firefox 似乎由于某种原因无法创建该文件,并且无法识别返回的 Windows 错误代码(它只知道 http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsLocalFileWin.cpp#240)。如果您尝试手动创建文件,例如使用记事本,这可能是最好的 - 我想您也会在那里收到错误,但会显示更多信息。
Your code works correctly for me, no problems here. It seems that Firefox cannot create the file for some reason and it doesn't recognize the Windows error code returned (it only knows the error codes listed under http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsLocalFileWin.cpp#240). It is probably best if you try to create the file manually, e.g. with Notepad - I guess that you will get an error there as well but with a more informative message.