function imgChange(obj1, obj2) { //获取点击的文本框 var file = document.getElementById("file"); //存放图片的父级元素 var imgContainer = document.getElementsByClassName(obj1)[0]; //获取的图片文件 var fileList = file.files; if(fileList.length >4 ){ Alert('不能超过四张照片'); return false; } //文本框的父级元素 var input = document.getElementsByClassName(obj2)[0]; var imgArr = []; //遍历获取到得图片文件 for (var i = 0; i < fileList.length; i++) { var imgUrl = window.URL.createObjectURL(file.files[i]); imgArr.push(imgUrl); var img = document.createElement("img"); img.setAttribute("src", imgArr[i]); var imgAdd = document.createElement("div"); imgAdd.setAttribute("class", "z_addImg"); imgAdd.appendChild(img); imgContainer.appendChild(imgAdd); }; imgRemove(); };
如果一次性选多张的话应该就能获取多张 如果是一次一次点击添加 就只能 获取最后一张图片
这个问题解决就完美了,等答案
删除本张图片再次上传本张图片上传不了,怎么解决呢
unction imgremove() { var imglist = document.getelementsbyclassname("z_addimg"); var mask = document.getelementsbyclassname("z_mask")[0]; var cancel = document.getelementsbyclassname("z_cancel")[0]; var sure = document.getelementsbyclassname("z_sure")[0]; f (var j = 0; j < imglist.length; j++) { imglist[j].index = j; imglist[j].onclick = function() { var t = this; mask.style.display = "block"; cancel.onclick = function() { mask.style.display = "none"; }; sure.onclick = function(){ mask.style.display = "none"; t.setattribute("id", "remove"); $("#remove").remove(); }; } }; };
如果一次性选多张的话应该就能获取多张 如果是一次一次点击添加 就只能 获取最后一张图片
这个问题解决就完美了,这个问题能解决吗??????上传多张图片,后台只能接收到第一张,怎么回事??后台该怎么接收??
后台怎么获取不到值呀
前台用formdata
var formData = new FormData($( "#exeriseForm" )[0]); var imgg = []; imgg = $('.z_addImg img'); var imgSrc = []; var imgBase64 = []; for (var i = 0; i < imgg.length; i++) { if (imgg[i].src != 'undefined') { imgSrc.push(imgg[i].src) var image = new Image(); image.src = imgg[i].src; var base64 = getBase64Image(image); imgBase64.push(base64); formData.append("imageName", base64.split(',')[1]); } }
后台
String[] str = request.getParameterValues("imageName");
就可获取到值,让后用上传base64的图片就行了
function getBase64Image(img) { var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, img.width, img.height); var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase(); var dataURL = canvas.toDataURL("image/" + ext); return dataURL; }