<input type="file">图片上传时,先预览

<label>
    <input type="file" id="upload">
</label>    

js

$("#upload").on('change',function(){
        var file = this.files[0];
        console.log(this.files);
        var reader = new FileReader();

        //读取文件过程方法
        reader.onloadstart = function (e) {
            console.log("开始读取....");
        }
        reader.onprogress = function (e) {
            console.log("正在读取中....");
        }
        reader.onabort = function (e) {
            console.log("中断读取....");
        }
        reader.onerror = function (e) {
            console.log("读取异常....");
        }
        reader.onload = function (e) {
            console.log("成功读取....");
            var img = "<img src='"+e.target.result+"'>";
            $("#upload").parent().before(img);
            console.log(e.target.result);
        }

        reader.readAsDataURL(file)
    })

 ####在input框之前插入预览图片

posted @ 2019-01-21 16:33  董七  阅读(315)  评论(0编辑  收藏  举报