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

邮差的信 提交于 2019-11-27 13:44:01

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

<input class="selectImg" style="position:absolute;opacity: 0;width: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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!