File write in Netscape 7 / Mozilla

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Szlazak

    #1

    File write in Netscape 7 / Mozilla

    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("sign ed.applets.code base_principal_ support", true);

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

    grant codeBase "file:///c:/mywebpagefilesd ir/*" {
    permission java.security.A llPermission;
    }

    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.netscap e && navigator.javaE nabled) {

    netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileWrite')
    ;
    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.FileWri ter(fn);
    fr.write (txt);
    fr.close();
    return true;
    }
    catch(e) {
    alert('Permissi on to write to file was denied.');
    return false;
    }
    }
    else
    return false;
    }
    else if (document.all) {
    var fs = new ActiveXObject(' Scripting.FileS ystemObject');
    if (fs.FileExists( fn))
    if (!confirm('file ' + fn + ' exists. Overwrite?'))
    fn = prompt ('new file name: ', fn);
    if (fn) {
    var fr = fs.CreateTextFi le (fn, true);
    fr.write (txt);
    fr.close();
    return true;
    }
    else
    return false;
    }
    }

    var fileName = 'c:/mywebpagefilesd ir/test.html';

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

    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!
  • Randell D.

    #2
    Re: File write in Netscape 7 / Mozilla


    "Mark Szlazak" <anonymous@devd ex.com> wrote in message
    news:3f103e08$0 $197$75868355@n ews.frii.net...[color=blue]
    > 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("sign ed.applets.code base_principal_ support", true);
    >
    > I've also added this to my java.policy files:
    >
    > grant codeBase "file:///c:/mywebpagefilesd ir/*" {
    > permission java.security.A llPermission;
    > }
    >
    > 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.netscap e && navigator.javaE nabled) {
    >
    > netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileWrite')
    > ;
    > 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.FileWri ter(fn);
    > fr.write (txt);
    > fr.close();
    > return true;
    > }
    > catch(e) {
    > alert('Permissi on to write to file was denied.');
    > return false;
    > }
    > }
    > else
    > return false;
    > }
    > else if (document.all) {
    > var fs = new ActiveXObject(' Scripting.FileS ystemObject');
    > if (fs.FileExists( fn))
    > if (!confirm('file ' + fn + ' exists. Overwrite?'))
    > fn = prompt ('new file name: ', fn);
    > if (fn) {
    > var fr = fs.CreateTextFi le (fn, true);
    > fr.write (txt);
    > fr.close();
    > return true;
    > }
    > else
    > return false;
    > }
    > }
    >
    > var fileName = 'c:/mywebpagefilesd ir/test.html';
    >
    > if (writeToFile(fi leName,
    > '<html><head>< \/head><body><h2> Hello!<\/h2><\/body><\/html>'))
    > window.open(fil eName);
    >
    > Specific help in making this work would be much appreciated![/color]


    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.


    Comment

    • Mark Szlazak

      #3
      Re: File write in Netscape 7 / Mozilla

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

      function xulFileWrite(fi lePath, content) {
      if (window.netscap e)
      try {
      netscape.securi ty.PrivilegeMan ager.enablePriv ilege("Universa lXPConnect
      ");
      var file =
      Components.clas ses["@mozilla.o rg/file/local;1"].createInstance (Component
      s.interfaces.ns ILocalFile);
      file.initWithPa th(filePath);
      if (!file.exists() ) {
      alert('Creating new file ' + filePath);
      file.create(0x0 0, 0644);
      }
      var outputStream =
      Components.clas ses["@mozilla.o rg/network/file-output-stream;1"].createIn
      stance(Componen ts.interfaces.n sIFileOutputStr eam);
      outputStream.in it(file, 0x20 | 0x04, 00004, null);
      outputStream.wr ite(content, content.length) ;
      outputStream.fl ush();
      outputStream.cl ose();
      return true;

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

      if (xulFileWrite(' D:\\My Web Pages\\test.htm l',
      '<html><head>< \/head><body><h2> Hello!<\/h2><\/body><\/html>'))
      window.open('fi le:\/\/\/D:\\My Web Pages\\test.htm l', 'testWindow',
      'left=20,top=11 0,width=500,hei ght=200');


      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...