Categories
Mozilla

Write File

Can anyone tell me why this code doesn’t work in Thunderbird 0.9 (I haven’t tried Firefox 1.0).

Note: There is a slash in the directory path C:, for some reason wordpress is fudging it up. MovableType did this crud too. So it’s not showing, but it’s there in the code.

function sync()
{
testFileOutputStream("C:\foo.txt", "la la la \n robert");
}
 
function testFileOutputStream(path, buffer) {
  // file is nsIFile, data is a string
  var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    .createInstance(Components.interfaces.nsIFileOutputStream);
 
  // use 0x02 | 0x10 to open file for appending.
  stream.init(nsLocalFile(path), 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
  stream.write(buffer, buffer.length);
  stream.close();
}
 
function nsLocalFile(path)
{
    const LOCALFILE_CTRID = "@mozilla.org/file/local;1";
    const nsILocalFile = Components.interfaces.nsILocalFile;
 
    var localFile =
        Components.classes[LOCALFILE_CTRID].createInstance(nsILocalFile);
    localFile.initWithPath(path);
    return localFile;
}

Keep getting this error:

Error: uncaught exception: [Exception… “Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIFileOutputStream.init]” nsresult: “0x80004005 (NS_ERROR_FAILURE)” location: “JS frame :: chrome://extension/content/extension.js :: testFileOutputStream :: line 12” data: no]

Simply want to be able to write to C: (or any place on my HD that I specify). Prefer not to use JSlib, simply because I don’t want dependencies. It’s really borderline insane to think you can’t do something as simple as write out to a file without depending on other lib’s.

Thanks in advance.

Comments/Email as usual.

3 replies on “Write File”

Hmm don’t really have time to look over that code, but why don’t you use MonkeySagee’s IO lib, it’s small and has everything you could need.
I know it works in TB as that is what we use for QuickNote.

If you’d rather not use it, I’m sure it will point you in the right direction on what you need to add/change.

http://www.mozdev.org/source/b.....web-markup

Cheers
-Jed πŸ™‚

Wild speculation: you said there was a backslash, but don’t you need to put a double-backslash? Otherwise the \ just escapes (needlessly) the : following it and never actually makes it anywhere… so it would be attempting to output to “C:foo.txt” ?

Of course you might well be doing that already, in which case ignore this comment πŸ˜‰

I have been anxiously awaiting Google’s web browser’s widgets for months it seems – At this point I have played with 5 extensions and am awed with the plugins. FF has always slowed down & the extensions are welcomed.

Leave a Reply to Antony Mawer Cancel reply

Your email address will not be published. Required fields are marked *