这几天用js写一个读写文件的程序,在此记录一下遇到的问题~~html
最初的程序以下:spring
//读文件浏览器
function readFile(filename){app
var fso = new ActiveXObject("Scripting.FileSystemObject"); 函数
var f = fso.OpenTextFile(filename,1); oop
var s = "";测试
while (!f.AtEndOfStream)firefox
s += f.ReadLine()+"\n";htm
f.Close();ip
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包含当前路径信息,不如用它作相应处理以得到当前路径,再和须要打开的文件连在一块不就成了绝对路径了?捏哈哈~~~
因而有了如下代码:
//获取绝对地址
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,后来发如今IE七、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"); 也就没什么用了~~唉~~~
在传相对路径参数时,要用右斜杠,还要转义,也就是要用两个右斜杠分隔……用左斜杠或者单斜杠都不行,我不知道是我比较特殊仍是你们都这样~~~