Write to a textfile using Javascript
Posted
by karikari
on Stack Overflow
See other posts from Stack Overflow
or by karikari
Published on 2010-01-05T08:38:34Z
Indexed on
2010/03/17
7:01 UTC
Read the original article
Hit count: 312
JavaScript
Under Firefox, I want to do something like this :
I have a .htm file, that has a button on it. This button, when I click it, the action will write a text inside a local .txt file. By the way, my .htm file is run locally too.
I have tried multiple times using this code, but still cant make my .htm file write to my textfile:
function save() {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("Permission to save file was denied.");
}
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( savefile );
if ( file.exists() == false ) {
alert( "Creating file... " );
file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
}
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance( Components.interfaces.nsIFileOutputStream );
outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var output = 'test test test test';
var result = outputStream.write( output, output.length );
outputStream.close();
}
This part is for the button:
<input type="button" value="write to file2" onClick="save();">
© Stack Overflow or respective owner