javascript简单代码实现图片上传预览

废话不多说,直接贴代码

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
}

作为笔记用,先贴上后面再解释

用法

html:

$('.uploadimg').on('change', function() { var imgUrl = getObjectURL(this.files[0]); $(".imgPrvew").attr('src', imgUrl) }); 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 }

先这样....

你可能感兴趣的:(javascript简单代码实现图片上传预览)