Skip to content Skip to sidebar Skip to footer

Auto Save A File In Firefox

I am trying to find a way where by we can auto save a file in Firefox using JS. The way I have done till yet using FireShot on a Windows Desktop: var element = content.document.cre

Solution 1:

If you are creating a Firefox add-on then FileUtils and NetUtil.asyncCopy are your friends:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");

var TEST_DATA = "this is a test string";
var source = Components.classes["@mozilla.org/io/string-input-stream;1"].
                 createInstance(Components.interfaces.nsIStringInputStream);
source.setData(TEST_DATA, TEST_DATA.length);

var file = new FileUtils.File("c:\\foo\\bar.txt");
var sink = file.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY |
                                               FileUtils.MODE_CREATE);
NetUtil.asyncCopy(source, sink);

This will asynchronously write the string this is a test string into the file c:\foo\bar.txt. Note that NetUtil.asyncCopy closes both streams automatically, you don't need to do it. However, you might want to pass a function as third parameter to this method - it will be called when the write operation is finished.

See also: Code snippets, writing to a file

Solution 2:

Every computer has a different file structure. But still, there is a way. You can save it to cookie / session, depends on how "permanent" your data wants to be.

Do not consider writing a physical file as it requires extra permission.

Post a Comment for "Auto Save A File In Firefox"