layui实现多图片上传并限制上传的图片数量
人气:0废话不多说了,直接上代码吧!
//给图片添加删除 function mouseChange() { $(document).on("mouseenter mouseleave", ".file-iteme", function (event) { if (event.type === "mouseenter") { //鼠标悬浮 $(this).children(".info").fadeIn("fast"); $(this).children(".handle").fadeIn("fast"); } else if (event.type === "mouseleave") { //鼠标离开 $(this).children(".info").hide(); $(this).children(".handle").hide(); } }); } mouseChange(); //json的length function getJsonLength(jsonData){ var jsonLength = 0; for(var item in jsonData){ jsonLength++; } return jsonLength; } //多图片上传 var ImgList = $('#uploader-list'),uploadListIns =upload.render({ elem: '#chooseImg', url: $("#projectUrl").val()+ '/img/imgUpload.do', accept: 'images', acceptMime: 'image/jpg,image/png,image/jpeg', exts: 'jpg|png|jpeg', size: 1024, multiple: true, auto: true, choose: function(obj){ var files = obj.pushFile(); //将每次选择的文件追加到文件队列 var len = getJsonLength(files); //读取本地文件 obj.preview(function (index, file, result) { if (parseInt(len)+parseInt(coachPicsArray.length)-count > 6){ layer.msg("门店图片不能超过6张"); //遍历 $.each(files,function(_key){ var key = _key; var value = files[_key]; if(_key == index) { //删除 delete files[_key]; } }); }else { var reader = new FileReader(); reader.onload = function (e) { var image = new Image(); image.onload = function () { var realWidth = image.width; var realHeight = image.height; var tr = $(['<div id="upload-' + index + '" class="file-iteme">' + '<div class="removeIcon handle"> <i class="layui-icon" style="color: white ;margin-right: 40%"></i></div>' + '<img style="color: white;width: 120px" "showBig(this)" src=' + result + ' id="img-' + index + '">' + '</div>'].join('')); //删除 tr.find('.handle').on('click', function () { $(this).parent().remove(); delete files[index]; //删除对应的文件 var value = $("#clubCoachPics").val().split(","); var arr = []; for( var i in value){ if (value[i] != $(this).next().data('value')){ arr.push(value[i]); } } $("#clubCoachPics").val(arr.join(",")) ; }); ImgList.append(tr); }; image.src = result; }; //正式读取文件 reader.readAsDataURL(file); } }); }, before: function (obj) { layer.msg('图片上传中...', { icon: 16, shade: 0.01, time: 3000 }) }, done: function (res, index, upload) { $("#img-"+ index + "").attr("data-value",res.imgUrl); if(res.code == 0){ //上传成功 var imgUrl = $("#clubCoachPics").val(); if(imgUrl==null||imgUrl==""){ $("#clubCoachPics").val(res.imgUrl); }else{ $("#clubCoachPics").val(imgUrl+","+res.imgUrl); } delete files[index]; //删除文件队列已经上传成功的文件 return; }; } });
以上这篇layui实现多图片上传并限制上传的图片数量就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
您可能感兴趣的文章:
加载全部内容