js 读写文件

这几天用js写一个读写文件的程序,在此记录一下遇到的问题~~

最初的程序如下:

//读文件

function readFile(filename){

    var fso = new ActiveXObject("Scripting.FileSystemObject"); 

    var f = fso.OpenTextFile(filename,1); 

    var s = "";

     while (!f.AtEndOfStream)

           s += f.ReadLine()+"\n";

     f.Close();

     return s;

}

//写文件

function writeFile(filename,filecontent){

     var fso, f, s ;

     fso = new ActiveXObject("Scripting.FileSystemObject"); 

     f = fso.OpenTextFile(filename,8,true);

     f.WriteLine(filecontent);

     f.Close();

     alert('ok');

}

      这两个函数在参数filename为绝对路径时是没问题的,但是若为相对路径则无法正确运行~~~在网上查了不少资料,有人说OpenTextFile()的第一个参数既可以是绝对路径也可以是相对路径,也有人说只能是绝对路径~~

      无论孰是孰非,我确实是用了相对路径时就无法正确运行。但是程序的移植性又要求参数不能直接传绝对路径~

      后来想到document.location.pathname包含当前路径信息,不如用它做相应处理以获得当前路径,再和需要打开的文件连在一块不就成了绝对路径了?捏哈哈~~~js 读写文件 - spring - 成功之门

      于是有了以下代码:

//获取绝对地址

function abPath(){

     var absolutePath = '';

     var pathArray = new Array(); 

     var currentFileLocation = document.location.pathname;    //alert(currentFileLocation);

     pathArray = currentFileLocation.split("\\");

     pathArray[0] = pathArray[0].substr(1,pathArray[0].length-1);

/***********************************************************/

/*做这样的处理是因为alert(currentFileLocation)弹出:*/

/*        /D:\alpha\test.html      */

/*   当然test.html是调用这些函数的当前页面啦 */

/***********************************************************/

     for(var i = 0; i<pathArray.length; i++){

            if(pathArray[i] != 'test.html')

            absolutePath = absolutePath + pathArray[i] + "\\";

     }//alert(absolutePath); 

     return absolutePath;

}

//读文件

function readFile(filename){

      var absolutePath = abPath();

      filename = absolutePath + filename;  //alert(filename); 

      var fso = new ActiveXObject("Scripting.FileSystemObject");

      var f = fso.OpenTextFile(filename,1);

      var s = "";

      while (!f.AtEndOfStream)

      s += f.ReadLine()+"\n";

      f.Close();

      s = parseInt(s);

      return s;

}

//写文件

function writeFile(filename,filecontent){

      var absolutePath = abPath();

      filename = absolutePath + filename;   //alert(filename); 

      var fso = new ActiveXObject("Scripting.FileSystemObject");  

      var f = fso.OpenTextFile(filename,2,true);

      f.WriteLine(filecontent); 

      f.Close();

}

这下好了,调用的时候可以传相对路径了~~~~

      我用来测试的浏览器是IE6,后来发现在IE7、IE8中却又无法正确运行了~~~~用alert(currentFileLocation)一测试,发现它们弹出的路径信息为:/D:/alpha/test.html,与IE6的斜杠方向不同,所以要做不同的处理,同时还要增加浏览器版本判断,代码如下:

//判断是否ie6

function isIe6(){

   var browserName = navigator.appName;   //alert(browserName);

   var browserVer = navigator.userAgent.toLowerCase();

   if(browserName == "Microsoft Internet Explorer"){

      if(browserVer.match(/msie ([\d.]+)/)[1] < 7) return true;

   }

   else return false;

}

//获取绝对地址

function abPath(){

    var absolutePath = '';

    var pathArray = new Array();

    var loopindex = 0;

 

    var currentFileLocation = document.location.pathname;  alert(currentFileLocation);

 if(isIe6()){

   pathArray = currentFileLocation.split("\\");

   pathArray[0] = pathArray[0].substr(1,pathArray[0].length-1);

 } else {

   pathArray = currentFileLocation.split("/");

 }

 if(pathArray[0] == '')loopindex = 1;

 for(loopindex; loopindex<pathArray.length; loopindex++){

    if(pathArray[loopindex] != 'test.html')

       absolutePath = absolutePath + pathArray[loopindex] + "\\";

 }//alert(absolutePath); 

 return absolutePath;

}

//读文件

function readFile(filename){

    var absolutePath = abPath();

    filename = absolutePath + filename;  //alert(filename); 

    var fso = new ActiveXObject("Scripting.FileSystemObject");

    var f = fso.OpenTextFile(filename,1);

    var s = "";

    while (!f.AtEndOfStream)

        s += f.ReadLine()+"\n";

    f.Close();

   s = parseInt(s);

   return s;

}

//写文件

function writeFile(filename,filecontent){

    var absolutePath = abPath();

    filename = absolutePath + filename;  //alert(filename); 

    var fso = new ActiveXObject("Scripting.FileSystemObject");  

    var f = fso.OpenTextFile(filename,2,true);

    f.WriteLine(filecontent); 

    f.Close();

}  

       到此IE浏览器都可以正确读写文件了,有个缺憾就是在firefox浏览器中不能正确运行,因为firefox浏览器貌似不支持ActiveXObject控件,因而var fso = new ActiveXObject("Scripting.FileSystemObject"); 也就没什么用了~~唉~~~

       在传相对路径参数时,要用右斜杠,还要转义,也就是要用两个右斜杠分隔……用左斜杠或者单斜杠都不行,我不知道是我比较特殊还是大家都这样~~~js 读写文件 - spring - 成功之门

转载于:https://www.cnblogs.com/kick-ass/p/3427741.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值