IE浏览器下用JS创建文件

<!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=gb2312" />
<title>IE浏览器下用JS创建文件</title>
<script type="text/javascript">
function logInfo(title,text){
   var fso, tf;
   var folderPath="D:\\jslog";
   var logFileName="D:\\jslog\\"+getDateStr()+".txt";   //20150922.txt
   fso = new ActiveXObject("Scripting.FileSystemObject");//获取对象
   if(!checkFolderExists(fso,folderPath)){//判断是否存在该日志文件夹,不存在就创建
         fso.CreateFolder(folderPath);//创建
   }   
   if(checkFileExists(fso,logFileName)){             
            tf = fso.OpenTextFile(logFileName, 8, true);
   }else{
            tf = fso.CreateTextFile(logFileName, true);//创建一个文件
   }  
   // 写一行,并且带有新行字符。
   tf.WriteLine(title);   
   tf.WriteLine(getDateStr("DateTime")+text);
   tf.WriteLine();
   tf.Close();//关闭    
}
//检查文件夹是否存在,是true否false
function checkFolderExists(fso,folderPath){    
    if(fso.FolderExists(folderPath)){        
        return true;
    }else{        
        return false;    
    }
}
//检查文件是否存在,是true否false
function checkFileExists(fso,filePath){    
    if(fso.FileExists(filePath)){
        return true;
    }else{
        return false;    
    }
}
//获取当前日期
function getDateStr(type){
    var d = new Date(new Date().getTime());
    var year=d.getFullYear(); 
    var month=d.getMonth()+1; 
    var date=d.getDate(); 
    var hour=d.getHours(); 
    var minute=d.getMinutes(); 
    var second=d.getSeconds(); 
    if(month<10){
        month="0"+month;
    }
    if(date<10){
        date="0"+date;
    }
    if("DateTime"==type){
        return "["+year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second+"] ";
    }
    return year+""+month+""+date;
}
</script>
</head>

<body>
<!--仅支持IE浏览器-->
<input type="button" onclick="logInfo('printerror','this is printerror recorder')" value="JS创建文件"/>
</body>
</html>

 

参考文章:https://msdn.microsoft.com/zh-cn/library/314cz14s

 

posted @ 2015-09-22 16:53  喵喵2023  Views(372)  Comments(0Edit  收藏  举报