打开TXT文件并显示

<!DOCTYPE html>
<html>
<head>
    <title></title>  
    <script>
        function upload(input){
            //支持chrome IE10
            if (window.FileReader){
                var file = input.files[0];
                filename = file.name.split(".")[0];
                var reader = new FileReader();
                reader.onload = function (){
                    debugger;
                    console.log(this.result)
                    document.getElementById("ar1").value = this.result;
                }
                reader.readAsText(file,"utf-8"); //编码格式 
            }
                //支持IE 7 8 9 10
            else if (typeof window.ActiveXObject != 'undefined'){
                var xmlDoc;
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(input.value);
                document.getElementById("ar1").value = xmlDoc.xml;
            }
                //支持FF
            else if (document.implementation && document.implementation.createDocument){
                var xmlDoc;
                xmlDoc = document.implementation.createDocument("", "", null);
                xmlDoc.async = false;
                xmlDoc.load(input.value);
                document.getElementById("ar1").value = xmlDoc.xml;
            } else{
                alert('error');
            }         
        }
    </script>

</head>
<body>
    <input type="file" value="打开TXT文件" onchange="upload(this)" />
    <textarea name="ar1" id="ar1" cols="100" rows="20"></textarea>
</body>
</html>

 

posted @ 2016-01-04 15:14  风中起舞  阅读(394)  评论(0编辑  收藏  举报