function readfile(){
  var fso,str,file;
  getFname();
  fso = new ActiveXObject("Scripting.FileSystemObject");
  str = "没有打开文件";
  if (fso.FileExists(fname)){
   file=fso.OpenTextFile(fname,1,true,-2);
   str=file.readall();
   document.write(str);   
  
  }
 }
打开的文本文件是乱码
改成file=fso.OpenTextFile(fname,1,true,-1);
改成file=fso.OpenTextFile(fname,1,true,0);
输出均有问题
还有,使用type XX.txt输出也是乱码但是使用记事本打开的能正常显示~~
急求助

解决方案 »

  1.   

    补充,txt是由java程序输出
    输出源代码为:public synchronized static boolean WriteFile(String path, String Tcontent,boolean append) {

    if (path != null && path.trim().length() != 0) {
    try{
    FileOutputStream fos = new FileOutputStream(path, append);
    OutputStreamWriter ow = new OutputStreamWriter(fos, "utf-8");
    PrintWriter pw = new PrintWriter(ow);
    pw.write(Tcontent.toString());
    pw.flush();
    pw.close();
    ow.close();
    fos.close();
    return true;
    } catch (Exception eq) {
    eq.printStackTrace();
    return false;
    } } else {
    return false;
    }
    }
      

  2.   


    function readfile(fname){
    var fso,str,file;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    str = "没有打开文件";
    if (fso.FileExists(fname)){
    file=fso.OpenTextFile(fname,1,true,-1);
    str=file.readall();
    }
    document.write(str);
    }
    readfile("d:\\1.txt");
    我这里显示正常
      

  3.   

    乱码就是编码问题啊.第四个参数不就是针对编码的么.把-2改成0或者-1试试.我这里改成0就可以了
    file=fso.OpenTextFile(fname,1,true,0);
      

  4.   

    代码如下:
    <script>
    function readfile(){
      var fso,str,file;
      fname="a.txt";
      fso = new ActiveXObject("Scripting.FileSystemObject");
      str = "没有打开文件";
      if (fso.FileExists(fname)){
       file=fso.OpenTextFile(fname,1,true,-2);
       str=file.readall();
       document.write(str);   
      
      }
     }
     readfile();
     </script>我运行了这个代码在我机器上的IE6,什么内容都没有,就只有一个提示是否交互,奇怪了。 
      

  5.   

    文件本身的编码问题,记事本打开,另存为utf-8或者ansi进行测试
      

  6.   

    txt文本用了utf-8格式,内容如下:
    fso OpenTextFile方法说明文档信息 1页 转贴至 人人网QQ空间更多...举报 ... fso(524) filename(2388) format(6771) create(17079) object(13278) iomode...
    JS的html也是utf-8格式,而且在meta里也设置编码为utf-8,可是显示的时候却是这样:
    没有打开文件锘縡so OpenTextFile鏂规硶璇存槑鏂囨。淇℃伅 1椤?杞创鑷?浜轰汉缃慟Q绌洪棿鏇村...涓炬姤 ... fso(524) filename(2388) format(6771) create(17079) object(13278) iomode... 
    附加HTML内容如下:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>222</title>
    <script>
    function readfile(){
      var fso,file,str="";
      fname="b.txt";
      fso = new ActiveXObject("Scripting.FileSystemObject");
      str = "没有打开文件";
      if (fso.FileExists(fname)){
      file=fso.OpenTextFile(fname,1,true,0);
      while (!file.AtEndOfStream)
      {
      str=str+file.readall();
     
      }
       document.write(str);   
      }
     }
     readfile();
     </script></head>
    <body>
    </body>
    </html>