Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > File write in Netscape 7 / Mozilla

Reply
Thread Tools

File write in Netscape 7 / Mozilla

 
 
Mark Szlazak
Guest
Posts: n/a
 
      07-12-2003
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!
 
Reply With Quote
 
 
 
 
Randell D.
Guest
Posts: n/a
 
      07-13-2003

"Mark Szlazak" <(E-Mail Removed)> wrote in message
news:3f103e08$0$197$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
Mark Szlazak
Guest
Posts: n/a
 
      07-16-2003
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Netscape 7.1 to Netscape 8 Skid Schermerhorn Firefox 2 09-05-2005 04:04 PM
.js file not loading in time for function call, problem only in Netscape/Mozilla/Firefox danny.myint@gmail.com Javascript 3 01-08-2005 01:36 PM
Accessing Netscape mail via Netscape mail client A. Toprak Computer Support 0 12-24-2004 06:46 AM
Netscape with JRE 1.5 beta 1 ignores Netscape signing Mickey Segal Java 1 05-21-2004 01:52 PM
websphere, oracle, netscape: netscape timeout Tom Java 0 08-01-2003 11:03 PM



Advertisments