<object id="fileDialog" width="0px" height="0px" classid="clsid:F9043C85-F6F2-101A-A3C9-08002B2F49FB" codebase="http://activex.microsoft.com/controls/vb5/comdlg32.cab">
</object>
<textarea id=TxtBody style="background-color:#EEEEEE;width:100%;height:100">无标题</textarea>
<br>
<input type=button value=加载 onclick="loadDoc()">
<input type=button value=保存 onclick="saveDoc()">
<script>
function loadDoc()
{
fileDialog.CancelError=true;
    try{
    fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
     fileDialog.ShowOpen();
    var fso=new ActiveXObject("Scripting.FileSystemObject");
    var reading=1;
    var f=fso.OpenTextFile(fileDialog.filename,reading);
    //window.confirm(f);
    var r=f.ReadAll();
    f.close();
    TxtBody.value=r;
    }catch(e){}
}
function saveDoc()
{
fileDialog.CancelError=true;
   try{
   fileDialog.Filter="HTM Files (*.htm)|*.htm|Text Files (*.txt)|*.txt";
   fileDialog.ShowSave();
   var fso=new ActiveXObject("Scripting.FileSystemObject");
   var f=fso.CreateTextFile(fileDialog.filename,true);
   f.write(TxtBody.value);
   f.Close();
   }
   catch(e){}
}
</script>
</body>

解决方案 »

  1.   

    可以用FSO读取,写入文件.<script>
    function WriteDemo(s1)
    {
       var fso, f, r
       var ForReading = 1, ForWriting = 2;
       fso = new ActiveXObject("Scripting.FileSystemObject")
       f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true)
       f.Write(s1);
       f.Close();
       document.all.txt.value="";
    }
    </script>
    <textarea name=txt></textarea>
    <input type=button value="Write" onclick="WriteDemo(document.all.txt.value)">
      

  2.   

    FSO<script>
    //读取文件.
    function ReadTextFile(sfile)
    {
        var ForReading = 1;
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        f = fso.OpenTextFile(sfile, ForReading);
        str = f.readAll()
        alert(str)
    }
    ReadTextFile("c:\\testfile.txt")
    </SCRIPT>
      

  3.   

    请问用FSO读取,写入文件.能不能让用户选择保存路径和输入文件名,在保存阿。