Can I write/read local file on hard drive in FireFox (Netscape) ?

Discussion in 'Javascript' started by Alex, Mar 1, 2006.

  1. Alex

    Alex Guest

    Yes you can:

    <html><head><script language="javascript">

    SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
    alert(read('C:\\temp\\test.txt'));

    function SaveToFile (text, fileName) {
    try
    {netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');}
    catch (e) {alert("Permission to write file denied."); return 0;}
    var file =
    Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(fileName);
    if (!file.exists()) file.create(0x00, 0644);
    var outputStream =
    Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
    outputStream.init(file,0x20 | 0x02,00004,null);
    outputStream.write(text, text.length);
    outputStream.flush();
    outputStream.close();
    }

    function read(myfile) {
    try
    {netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}
    catch (e) {alert("Permission to read file denied."); return '';}
    var file =
    Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(myfile);
    if (!file.exists()) {alert("File not found."); return '';}
    var is =
    Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(
    Components.interfaces.nsIFileInputStream );
    is.init(file,0x01, 00004, null);
    var sis =
    Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(
    Components.interfaces.nsIScriptableInputStream );
    sis.init(is);
    var output = sis.read(sis.available());
    return output;
    }

    </script></head><body></body></html>
    Alex, Mar 1, 2006
    #1
    1. Advertising

  2. Alex

    Randy Webb Guest

    Re: Can I write/read local file on hard drive in FireFox (Netscape)?

    Alex said the following on 2/28/2006 11:03 PM:
    > Yes you can:


    Yes I can what?

    >


    No DTD.

    > <html><head><script language="javascript">


    Type attribute is mandatory, language attribute is deprecated.

    > SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt');
    > alert(read('C:\\temp\\test.txt'));
    >
    > function SaveToFile (text, fileName) {
    > try
    > {netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');}
    > catch (e) {alert("Permission to write file denied."); return 0;}


    And now, you have not written to my hard drive. Try again :)


    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Mar 1, 2006
    #2
    1. Advertising

  3. Alex wrote:

    > Yes you can:


    Nobody ever doubted this. In fact, this has been mentioned at least by me
    several times before. However, the script needs the privileges, and it
    can only get the privileges if it is signed, or, if it is not signed, the
    security settings of the client are reduced, which opens a security leak
    for all other Web sites.


    PointedEars
    Thomas 'PointedEars' Lahn, Mar 1, 2006
    #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. Laszlo Szijarto
    Replies:
    1
    Views:
    3,284
    Michael Earls
    Jan 23, 2004
  2. mh
    Replies:
    4
    Views:
    700
    Wolfgang Strobl
    May 31, 2005
  3. shailesh
    Replies:
    1
    Views:
    779
    Tim Golden
    Mar 28, 2007
  4. Peter
    Replies:
    8
    Views:
    157
    Evertjan.
    Oct 15, 2007
  5. michael
    Replies:
    1
    Views:
    90
    Bret Pettichord
    Oct 15, 2006
Loading...

Share This Page