js讀取文本內容


<!DOCTYPE html>
<html>
<head>
</head>
<body>
jsReadFile:<input type="file" onchange="jsReadFiles(this.files)"/>
<button onclick="jsReadFiles();">read</button>
</body>
<script src="jquery-3.2.0.min.js"></script>
<script>
//js 讀取文件
    function jsReadFiles(files) {
        if (files.length) {
            var file = files[0];
            var reader = new FileReader();//new一個FileReader實例
            if (/text+/.test(file.type)) {//判斷文件類型,是不是text類型
                reader.onload = function() {
                    $('body').append('<pre>' + this.result + '</pre>');
                }
                reader.readAsText(file);
            } else if(/image+/.test(file.type)) {//判斷文件是不是imgage類型
                reader.onload = function() {
                    $('body').append('<img src="' + this.result + '"/>');
                }
                reader.readAsDataURL(file);
            }
        }
    }
</script>
</html>

轉載不知名作者。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM