File write in Netscape 7 / Mozilla

Discussion in 'Javascript' started by Mark Szlazak, Jul 12, 2003.

  1. Mark Szlazak

    Mark Szlazak Guest

    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!





    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Mark Szlazak, Jul 12, 2003
    #1
    1. Advertising

  2. Mark Szlazak

    Randell D. Guest

    "Mark Szlazak" <> wrote in message
    news:3f103e08$0$197$...
    > 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.
     
    Randell D., Jul 13, 2003
    #2
    1. Advertising

  3. Mark Szlazak

    Mark Szlazak Guest

    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!
     
    Mark Szlazak, Jul 16, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospam
    Replies:
    4
    Views:
    1,717
    nandulwc
    Sep 9, 2009
  2. Tom
    Replies:
    0
    Views:
    495
  3. Mickey Segal
    Replies:
    1
    Views:
    666
    Mickey Segal
    May 21, 2004
  4. yaktipper
    Replies:
    10
    Views:
    281
    Richard Cornford
    Jul 10, 2003
  5. Replies:
    3
    Views:
    411
    ExGuardianReader
    Jan 8, 2005
Loading...

Share This Page