使用Javascript写入文本文件

时间:2010-01-05 08:38:35

标签: javascript

在Firefox下,我想做类似的事情:

我有一个.htm文件,上面有一个按钮。此按钮,当我单击它时,该操作将在本地.txt文件中写入文本。顺便说一句,我的.htm文件也在本地运行。

我已尝试多次使用此代码,但仍无法使我的.htm文件写入我的文本文件:

function save() {
try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
    alert("Permission to save file was denied.");
}
var file = Components.classes["@mozilla.org/file/local;1"]
    .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( savefile );
if ( file.exists() == false ) {
    alert( "Creating file... " );
    file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
}
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    .createInstance( Components.interfaces.nsIFileOutputStream );

outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var output = 'test test test test';
var result = outputStream.write( output, output.length );
outputStream.close();

}

这部分用于按钮:

<input type="button" value="write to file2" onClick="save();">

3 个答案:

答案 0 :(得分:1)

Javascript不允许访问硬盘驱动器,但您可以使用ActiveXObject使用Javascript创建或写入文本文件。

function writeToDisk(writeString) {

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile(theFile, true);
a.WriteLine(writeString);
a.Close();
}

快乐编码

答案 1 :(得分:0)

即使你在本地运行它,我怀疑Firefox会让你访问文件系统。但是,如果您创建扩展,它将能够访问文件系统。

答案 2 :(得分:0)

如果您遇到问题,请继续尝试此示例:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window
    id="mywindow"
    title="Find Files"
    orient="horizontal"
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">



<grid flex="1">
  <columns>
    <column flex="1"/>
    <column/>
  </columns>
  <rows>
    <row>
    <box height="80">
        <label value="PDE-Identity"/>
        <textbox id="pde" value="" multiline="true"/>
      <row>
        <label value="FirstName"/>
        <textbox id="fname" value="" multiline="true"/>
      </row>
        <button id="save" label="save" oncommand="save();"/>
    </box>
     </row>
    </rows>
</grid>

<!--<script>
read();
</script>-->

<script type="application/x-javascript">
<![CDATA[

var savefile = "c:\\mozdata.xml";

function save() {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File is created " );
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    /* Open flags
    #define PR_RDONLY       0x01
    #define PR_WRONLY       0x02
    #define PR_RDWR         0x04
    #define PR_CREATE_FILE  0x08
    #define PR_APPEND      0x10
    #define PR_TRUNCATE     0x20
    #define PR_SYNC         0x40
    #define PR_EXCL         0x80
    */
    /*
    ** File modes ....
    **
    ** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
    ** The 'mode' argument may be ignored by PR_Open on other platforms.
    **
    **   00400   Read by owner.
    **   00200   Write by owner.
    **   00100   Execute (search if a directory) by owner.
    **   00040   Read by group.
    **   00020   Write by group.
    **   00010   Execute by group.
    **   00004   Read by others.
    **   00002   Write by others
    **   00001   Execute by others.
    **
    */
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
    var output = document.getElementById('pde').value;
    //var out = document.getElementById('fanme').value;
    var result = outputStream.write( output, output.length );
    outputStream.close();
    alert( "File is updated" );


}

<!--
function read() {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to read file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert("File does not exist");
    }
    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() );
    document.getElementById('blog').value = output;
}
-->

]]>
</script>

</window>