By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

File write in Netscape 7 / Mozilla

P: n/a
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_suppo rt", 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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Mark Szlazak" <an*******@devdex.com> wrote in message
news:3f*********************@news.frii.net...
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_suppo rt", 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.
Jul 20 '05 #2

P: n/a
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');
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.