File write in Netscape 7 / Mozilla

M

Mark Szlazak

All files that I will be referring to are in the same file folder. I
need my javascipt file to write or save the contents of a textarea to
other local files. This is for local use only on one system.

I have no problem doing this in NN4 and IE.

However, I have problems with nn7 and moz1.4 even when I add this to my
prefs.js files:

user_pref("signed.applets.codebase_principal_support", true);

I've also added this to my java.policy files:

grant codeBase "file:///c:/mywebpagefilesdir/*" {
permission java.security.AllPermission;
}

Here's the javascript code for IE and NN7. I'm always getting the alert
'Permission to write to file was denied.'.

function writeToFile(fn, txt) {
if (window.netscape && navigator.javaEnabled) {

netscape.security.PrivilegeManager.enablePrivilege('UniversalFileWrite')
;
var f = new java.io.File(fn);
if (f.exists())
if (!confirm('file ' + fn + ' exists. Overwrite?'))
fn = prompt ('new file name: ', fn);
if (fn) {
try {
var fr = new java.io.FileWriter(fn);
fr.write (txt);
fr.close();
return true;
}
catch(e) {
alert('Permission to write to file was denied.');
return false;
}
}
else
return false;
}
else if (document.all) {
var fs = new ActiveXObject('Scripting.FileSystemObject');
if (fs.FileExists(fn))
if (!confirm('file ' + fn + ' exists. Overwrite?'))
fn = prompt ('new file name: ', fn);
if (fn) {
var fr = fs.CreateTextFile (fn, true);
fr.write (txt);
fr.close();
return true;
}
else
return false;
}
}

var fileName = 'c:/mywebpagefilesdir/test.html';

if (writeToFile(fileName,
'<html><head><\/head><body><h2>Hello!<\/h2><\/body><\/html>'))
window.open(fileName);

Specific help in making this work would be much appreciated!
 
R

Randell D.

Mark Szlazak said:
All files that I will be referring to are in the same file folder. I
need my javascipt file to write or save the contents of a textarea to
other local files. This is for local use only on one system.

I have no problem doing this in NN4 and IE.

However, I have problems with nn7 and moz1.4 even when I add this to my
prefs.js files:

user_pref("signed.applets.codebase_principal_support", true);

I've also added this to my java.policy files:

grant codeBase "file:///c:/mywebpagefilesdir/*" {
permission java.security.AllPermission;
}

Here's the javascript code for IE and NN7. I'm always getting the alert
'Permission to write to file was denied.'.

function writeToFile(fn, txt) {
if (window.netscape && navigator.javaEnabled) {

netscape.security.PrivilegeManager.enablePrivilege('UniversalFileWrite')
;
var f = new java.io.File(fn);
if (f.exists())
if (!confirm('file ' + fn + ' exists. Overwrite?'))
fn = prompt ('new file name: ', fn);
if (fn) {
try {
var fr = new java.io.FileWriter(fn);
fr.write (txt);
fr.close();
return true;
}
catch(e) {
alert('Permission to write to file was denied.');
return false;
}
}
else
return false;
}
else if (document.all) {
var fs = new ActiveXObject('Scripting.FileSystemObject');
if (fs.FileExists(fn))
if (!confirm('file ' + fn + ' exists. Overwrite?'))
fn = prompt ('new file name: ', fn);
if (fn) {
var fr = fs.CreateTextFile (fn, true);
fr.write (txt);
fr.close();
return true;
}
else
return false;
}
}

var fileName = 'c:/mywebpagefilesdir/test.html';

if (writeToFile(fileName,
'<html><head><\/head><body><h2>Hello!<\/h2><\/body><\/html>'))
window.open(fileName);

Specific help in making this work would be much appreciated!


I do not believe you can use Javascript to write to local or server/remote
disk - so I guess the problem you are having is java and not javascript
related hence I suggest you try the java ng.
 
M

Mark Szlazak

After looking through Mozilla's xul, javascript and jslib sources, this
is what I got to work.

function xulFileWrite(filePath, content) {
if (window.netscape)
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect
");
var file =
Components.classes["@mozilla.org/file/local;1"].createInstance(Component
s.interfaces.nsILocalFile);
file.initWithPath(filePath);
if (!file.exists()) {
alert('Creating new file ' + filePath);
file.create(0x00, 0644);
}
var outputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].createIn
stance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file, 0x20 | 0x04, 00004, null);
outputStream.write(content, content.length);
outputStream.flush();
outputStream.close();
return true;

}
catch (e) {
alert(e);
return false;
}
}

if (xulFileWrite('D:\\My Web Pages\\test.html',
'<html><head><\/head><body><h2>Hello!<\/h2><\/body><\/html>'))
window.open('file:\/\/\/D:\\My Web Pages\\test.html', 'testWindow',
'left=20,top=110,width=500,height=200');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top