Firefox 4 扩展 file.createUnique 异常:NS_ERROR_FAILURE

发布于 2024-11-16 03:45:51 字数 1210 浏览 4 评论 0原文

我开发了一个 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 技术交流群。

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

发布评论

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

评论(1

深空失忆 2024-11-23 03:45:51

您的代码对我来说工作正常,这里没有问题。 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.

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