zoukankan      html  css  js  c++  java
  • 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

    ##############阁下如果是抄袭,爬取文章作恶或误导他人的开发者,请阅读中国现行法律的相关处罚条例再动手,转载之前最好先验证#############
  • 相关阅读:
    Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
    Session,Cookie,jsessionid,Url重写
    PHP输出当前进程所有变量 / 常量 / 模块 / 函数 / 类
    table自适应宽度
    python学习笔记1(字符串操作)
    jquery-select选中
    dos命令大全
    dos命令之创建文件,文件夹
    PHP中逻辑运算符的高效用法---&&和||
    apache vhost
  • 原文地址:https://www.cnblogs.com/chuyuan/p/4829447.html
Copyright © 2011-2022 走看看