HTML5 FileReader用法:图片预览、文本文件预览等

<!DOCTYPE html>
<html>
<head lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title></title>

    </head>
    <body>
    <h3>文本选择预览</h3>
        <input type="file" id="file" οnchange="readAsText(this.files)"/>
        <div id="fileContent" style="background-color: #e3ceab;"></div>
        <script>
            function  readAsText(files)
            {
                if(files.length)
                {
                    var file = files[0];
                    var reader = new FileReader();
                    reader.onload = function(){
                        document.getElementById("fileContent").innerHTML = this.result;
                    };
                    reader.readAsText(file);
                }
            }
            function handleFiles(files){
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    var imageType = /image.*/;

                    if (!file.type.match(imageType)) {
                        continue;
                    }

                    var img = document.createElement("img");
                    img.classList.add("obj");
                    img.file = file;
                    preview.appendChild(img);

                    var reader = new FileReader();
                    reader.onload = (function(aImg){
                        return function(e){
                            aImg.src = e.target.result;
                        };
                    })(img);
                    reader.readAsDataURL(file);
                }
            }
        </script>
        <hr/>
        <h3>文本拖拽预览</h3>
        <div id="dropbox" style="width: 100%;min-height: 200px;background-color: #b2dba1;">
            <p>Drop Here</p>
            <div id="fileContent2" style="background-color: #a8ff60;"></div>
            <script>
                var dropbox = document.getElementById("dropbox");
                dropbox.addEventListener("dragenter", dragenter, false);
                dropbox.addEventListener("dragover", dragover, false);
                dropbox.addEventListener("drop", drop, false);

                function dragenter(e) {
                    e.stopPropagation();
                    e.preventDefault();
                }

                function dragover(e) {
                    e.stopPropagation();
                    e.preventDefault();
                }

                function drop(e) {
                    e.stopPropagation();
                    e.preventDefault();
                    var dt = e.dataTransfer;
                    var files = dt.files;
                    if(files.length){
                        var file = files[0];
                        var reader = new FileReader();
                        reader.onload = function()
                        {
                            document.getElementById("fileContent2").innerHTML = this.result;
                        };
                        reader.readAsText(file);
                    }
                }

            </script>
        </div>
        <hr/>
        <h3>图片预览</h3>
        <input type="file" multiple οnchange="handleFiles(this.files)"/>
        <div id="preview"></div>
        <script>
            var preview = document.querySelector("#preview");
            function handleFiles(files){
                console.info(files);
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    var imageType = /image.*/;

                    if (!file.type.match(imageType)) {
                        continue;
                    }

                    var img = document.createElement("img");
                    img.classList.add("obj");
                    img.file = file;
                    preview.appendChild(img);

                    var reader = new FileReader();
                    reader.onload = (function(aImg){
                        return function(e){
                            aImg.src = e.target.result;
                        };
                    })(img);
                    reader.readAsDataURL(file);
                }
            }
        </script>
    </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值