直接上代码

<!DOCTYPE html>
<html>
<head>
    <title>
    </title>
</head>
<body>
    jsReadFile:<input type="file" onchange="jsReadFiles(this.files)" />
    <button onclick="jsReadFiles();">read</button>
</body>
</html>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.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 {
                alert("请选择文本文件");
                return false;
            }
            //else if (/image+/.test(file.type)) {//判断文件是不是imgage类型
            //    reader.onload = function () {
            //        $('body').append('<img src="' + this.result + '"/>');
            //    }
            //    reader.readAsDataURL(file);
            //}
        }
    }
</script>

 

相关文章:

  • 2021-06-24
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-11-05
  • 2021-07-07
  • 2021-10-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-10-02
  • 2022-12-23
  • 2022-01-24
相关资源
相似解决方案