且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript - 上传图片显示在页面是如何实现的?

更新时间:2023-02-25 12:24:26

不用ajax返回小图的url,直接用js就可以实现,下面是实现代码:

先看效果

HTML

<input type="file" id="imgInp">
<image id="blah" style="width:100px; height:100px;"/>

JS


function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});