js多图片上传,获取多个图片base64的值显示到页面

码农天地 -
js多图片上传,获取多个图片base64的值显示到页面

上传前显示效果

上传后显示效果


$('#img_purchase').on('click',function(){  
    $('#purchase').click();  
}); 
$('#img_sale').on('click',function(){  
    $('#sale').click();  
}); 

function getBase64 (file,Callback) {
    let reader = new FileReader()
      let imgResult = ''
      reader.readAsDataURL(file)
      reader.onload = function () {
        imgResult = reader.result
      }
      reader.onerror = function (error) {
        Callback(error)
      }
        reader.onloadend = function () {
        Callback(imgResult)
      }
};

    $(document).on('change', '#purchase', function () {
        var filePath = $(this).val()
        console.log(filePath);
        var arr_prepath=this.files;
        console.log(arr_prepath);
        for (var i=0;i<arr_prepath.length;i++){
            getBase64(arr_prepath[i],function(res){
        console.log(res);
                $('.img_addpurchase').before('<div >'+
                        '![](__JHWIMAGES__/ewc.png)'+
                        '![]()'+
                    '</div>',$('.purchase_top').firstChild);
            })
        }
    });
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

加个好友,技术交流

1628738909466805.jpg