js讀取本地文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
        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() {  
                    console.log(this.result);  
                }  
                reader.readAsText(file);  
            }   
            //支持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);   
                console.log(xmlDoc.xml);   
            }   
            //支持FF  
            else if (document.implementation && document.implementation.createDocument) {   
                var xmlDoc;   
                xmlDoc = document.implementation.createDocument("", "", null);   
                xmlDoc.async = false;   
                xmlDoc.load(input.value);   
                console.log(xmlDoc.xml);  
            } else {   
                alert('error');   
            }   
        }  
    </script>
</head>
<body>
<input type="file" onchange="upload(this)" />  
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章