jq读取文本内容

<!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>

转自:http://www.cnblogs.com/lzyit/p/6641296.html

原文地址:https://www.cnblogs.com/withoutaword/p/7155367.html