html上传图片的预览功能实现

表单代码(仅取上传文件部分):

<input class="selectImg" style="position:absolute;opacity: 0;100px;height:100px;" type="file" name="coverChart" onchange="showImg(this)">

js代码:

// 图片预览
function showImg(obj) {
    $(".doShow").css("background-image", "url('" + getObjectURL(obj.files[0]) + "')");
}

// 不同浏览器获得图片url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) {
        url = window.createObjectURL(file) ;
    } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url;
}

参考链接:https://blog.csdn.net/man_zuo/article/details/83115212

原文地址:https://www.cnblogs.com/YeHuan/p/11366126.html