jQuery多图上传到服务器并修改

该博客介绍了一种使用jQuery实现的多图上传预览功能,允许用户点击添加图片进行叠加上传,并且能够单独删除每张图片。在修改时,只上传新增的图片,不重复发送已存在的图片数据。此解决方案适用于需要此类功能的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html部分

<!DOCTYPE html>
<html>
<title>jQuery多图片增删改查</title>
<body>
	<aside class="mask works-mask">
			<div class="mask-content">
				<p class="del-p ">您确定要删除图片吗?</p>
				<p class="check-p"><span class="del-com wsdel-ok">确定</span><span class="wsdel-no">取消</span></p>
			</div>
		</aside>
		
	<form action="shopUsers/profile" method="post" id="basicForm" class="smart-form"
			  enctype="multipart/form-data" name="basicForm" novalidate="novalidate">
							
		<div class="img-box full">
			<section class=" img-section">
				<p class="up-p"><span class="up-span">商户门头照</span></p>
				<div class="z_photo upimg-div clear" >
					<section class="z_file fl">
						<img src="${path}/upload/img/a11.png" class="add-img">
						<input type="file"  id="door" class="file" accept="image/*" multiple  />
					</section>
				</div>
			</section>
		</div>
		 <footer>
				<button type="button" class="btn btn-primary" οnclick="shopHistory()"> 提交</button>
			</footer>
		
	 </form>
<script src="js/jquery-1.8.3.min.js"></script>
</body>
</html>
js部门
var door = {};  //用于上传的图片流
$(function(){
    var delParent;
    var defaults = {
        fileType         : ["jpg","png","bmp","jpeg"],   // 上传文件的类型
        fileSize         : 1024 * 1024 * 10                  // 上传文件的大小 10M
    };
    /*点击图片的文本框*/
    $(".file").change(function(){
        var idFile = $(this).attr("id");
        var file = document.getElementById(idFile);
        var imgContainer = $(this).parents(".z_photo"); //存放图片的父亲元素
        var fileList = file.files; //获取的图片文件
        var input = $(this).parent();//文本框的父亲元素
        var imgArr = [];
        //遍历得到的图片文件
        var numUp = imgContainer.find(".up-section").length;
        var totalNum = numUp + fileList.length;  //总的数量
            if(fileList.length > 5 || totalNum > 5 ){
                alert("上传图片数目不可以超过5个,请重新选择");  //一次选择上传超过5个 或者是已经上传和这次上传的到的总数也不可以超过5个
            }
            else if(numUp < 5){
                fileList = validateUp(fileList);
                for(var i = 0;i<fileList.length;i++){
                    var imgUrl = window.URL.createObjectURL(fileList[i]);
                    imgArr.push(imgUrl);
                    var $section = $("<section class='up-section fl loading'>");
                    imgContainer.prepend($section);
                    var $span = $("<span class='up-span'>");
                    $span.appendTo($section);

                    var $img0 = $("<img class='close-upimg'>").on("click",function(event){
                        event.preventDefault();
                        event.stopPropagation();
                        $(".works-mask").show();
                        delParent = $(this).parent();
                    });
                    $img0.attr("src","../upload/img/a7.png").appendTo($section);
                    var $img = $("<img class='up-img up-opcity'>");
                    $img.attr("src",imgArr[i]);
                    $img.appendTo($section);
                    var $p = $("<p class='img-name-p'>");
                    $p.html(fileList[i].name).appendTo($section);

					 door[fileList[i].name] = fileList[i];	//流存储
					 
                    var $input = $("<input id='taglocation' name='taglocation' value='' type='hidden'>").attr("value",imgArr[i]);
                    $input.appendTo($section);
                    var $input2 = $("<input id='tags' name='tags' value='' type='hidden'/>");
                    $input2.appendTo($section);
                }
            }
            setTimeout(function(){
                $(".up-section").removeClass("loading");
                $(".up-img").removeClass("up-opcity");
            },450);
            numUp = imgContainer.find(".up-section").length;
            if(numUp >= 5){
                $(this).parent().hide();
            }
        }

    });






    $(".z_photo").delegate(".close-upimg","click",function(){
        $(".works-mask").show();
        delParent = $(this).parent();
    });

    $(".wsdel-ok").click(function(){
        $(".works-mask").hide();
        var numUp = delParent.siblings().length;
        if(numUp < 6){
            delParent.parent().find(".z_file").show();
        }
        var key=delParent.find(".img-name-p").html();
        var id=delParent.parent().find(".file").attr("id");
        if(id=='door'){
            delete door[key];
        }

        if(!isNaN(key)) {	//这里判断key是否在远端存在,不需要单独上传这步可以省略
            $.post("shopUsers/delImage",{id:key,state:2},function(data){
            });
        }

        delParent.remove();
    });

    $(".wsdel-no").click(function(){
        $(".works-mask").hide();
    });

    function validateUp(files){
        var arrFiles = [];//替换的文件数组
        for(var i = 0, file; file = files[i]; i++){
            //获取文件上传的后缀名
            var newStr = file.name.split("").reverse().join("");
            if(newStr.split(".")[0] != null){
                var type = newStr.split(".")[0].split("").reverse().join("");
                // 类型符合,可以上传
                if (file.size >= defaults.fileSize) {
                    alert(file.size);
                    alert('您这个"'+ file.name +'"文件大小过大');
                } else {
                    arrFiles.push(file);
                }
            }else{
                alert('您这个"'+ file.name +'"没有类型, 无法识别');
            }
        }
        return arrFiles;
    }
});

//  提交
 var shopHistory=function () {
     // 上传的流存储进要传递数据当中
     var fm=document.getElementById('basicForm');
     var test=new FormData(fm);
     for(var ab in door){
         test.append("door", door[ab]);
     }
    


    $.ajax({
        url:"shopUsers/profile",
        type:"post",
        data:test  ,
        cache: false,
        processData: false,
        contentType: false,
        async: false,
        success:function(resp){
            $form.find(".state-success").removeClass("state-success");
        }
    });
}




//遍历填充信息
var shopUsersImage=服务器返回的数据;
if (shopUsersImage) {
		//循环填充数据
		var numUp;
		$.each(shopUsersImage, function (index, value) {
			var ids = $("#" + value.type).parents(".z_photo");
			var $section = $("<section class='up-section fl'>");
			var $img0 = $("<img class='close-upimg'>").attr("src", "../upload/img/a7.png");
			var $img = $("<img class='up-img' width=190 height=180>").attr("src", "../" + value.imageMin);
			var $p = $("<p class='img-name-p'>").html(value.id);
			var $section0 = $(" </section>");
			$section.append($img0).append($img).append($p).append($section0);
			$(ids).prepend($section);

			//	判断数量是否超过允许上传量
			numUp = $(ids).find(".up-section").length;
			if (value.type == 'other') {
				if (numUp >= 10) {
					$("#" + value.type).parent().hide();
				}
			} else {
				if (numUp >= 5) {
					$("#" + value.type).parent().hide();
				}
			}
		});
	}
css部分

* {
	box-sizing: border-box;
	-moz-box-sizing: border-box;
	/* Firefox */
	-webkit-box-sizing: border-box;
	/* Safari */
}
.up-section .type-upimg{
	display: none;
}
::-ms-clear,::-ms-reveal{display:none;}
textarea{
	outline: none;
	line-height: 14px;
	padding-left: 4px;
	padding-top: 4px;
	border: 1px solid #ccc;
	color: #444;
	font-size: 14px;
	outline: none;
	text-align: left;
}
.overflow{
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
button{
	outline: none;
	border: 0px;
	font-family: "微软雅黑", "PingFang SC", "arial, helvetica, sans-serif";
}
input{
	font-family: "微软雅黑", "PingFang SC", "arial, helvetica, sans-serif";
	outline: none;
}
a {
	outline: none;
}
a,button{cursor:pointer;} 

body {
	background: #f4f4f4;
	font-size: 14px;
	font-family: "微软雅黑", "PingFang SC", "arial, helvetica, sans-serif";
}
.clear {
	clear: both;
}
.full{
	margin: 0 30px;
}
.full-big{
	margin: 0 auto;
}
.img-full{
	display: block;
	width: 80%;
}
::-webkit-input-placeholder {
	color: #777;
	font-size: 14px;
}
:-moz-placeholder {
	/* Firefox 18- */
	color: #777;
	font-size: 14px;
}
::-moz-placeholder {
	/* Firefox 19+ */
	color: #777;
	font-size: 14px;
}
:-ms-input-placeholder {
	color:#777;
	font-size: 14px;
}
/* ====clear float====== */
/*nav a:visited{color: rgb(65,65,65);}
aside a:visited{color: rgb(65,65,65);}*/
.fl {
	float: left;
}
.fr {
	float: right;
}
.clear:after {
	content: '';
	display: block;
	clear: both;
}
/* reset */
.pic img{display: none;}
i,
p,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
br,
em,
dl,
dd,
li,
ul,
ol,
td,
th,
pre,
form,
body,
input,
strong,
textarea,
select,figcaption,figure{
	margin: 0;
	padding: 0;
}
em {
	font-style: normal
}
li {
	list-style: none
}
a {
	text-decoration: none;
}
img {
	border: none;
	
}
table {
	border-collapse: collapse;
}
textarea {
	resize: none;
	overflow: auto;
}
a,button{cursor:pointer;} 


/*上传图片插件的样式*/
.img-box{
	margin-top: 40px;
}
.img-box .up-p{
	margin-bottom: 20px;
	font-size: 16px;
	color: #555;
}
.z_photo{
	padding: 18px;
	border:2px dashed #E7E6E6;
	/*padding: 18px;*/
}
.z_photo .z_file{
	position: relative;
}
.z_file  .file{
	width: 100%;
	height: 100%;
	opacity: 0;
	position: absolute;
	top: 0px;
	left: 0px;
	z-index: 100;
}
.z_photo .up-section{
	position: relative;
	margin-right: 20px;
	margin-bottom: 20px;
}
.up-section .close-upimg{
	position: absolute;
	top: 6px;
	right: 8px;
	display: none;
	z-index: 10;
}
.up-section .up-span{
	display: block;
	width: 100%;
	height: 100%;
	visibility: hidden;
	position: absolute;
	top: 0px;
	left: 0px;
	z-index: 9;
	background: rgba(0,0,0,.5);
}
.up-section:hover{
	border: 2px solid #f15134;
}
.up-section:hover .close-upimg{
	display: block;
}
.up-section:hover .up-span{
	visibility: visible;
}
.z_photo .up-img{
	display: block;
	width: 100%;
	height: 100%;
}
.loading{
    border: 1px solid #D1D1D1;
	background:url("/kuaibeiPro/manage/resources/js/plugs/msgbox/loading.gif") no-repeat center;
}
.up-opcity{
	opacity: 0;
}
.img-name-p{
	display: none;
}
.upimg-div .up-section {
    width: 190px;
    height: 180px;
}
.img-box .upimg-div .z_file {
    width: 190px;
    height: 180px;
}
.z_file .add-img {
    display: block;
    width: 190px;
    height: 180px;
}
/*遮罩层样式*/
.mask{
	z-index: 1000;
	display: none;
	position: fixed;
	top: 0px;
	left: 0px;
	width: 100%;
	height: 100%;
	background: rgba(0,0,0,.4);
}
.mask .mask-content{
	 width: 500px;
	 position: absolute;
	 top: 50%;
	 left: 50%;
	 margin-left: -250px;
	 margin-top: -80px;
	 background: white;
	 height: 160px;
	 text-align: center;
}
.mask .mask-content .del-p{
	color: #555;
	height: 94px;
	line-height: 94px;
	font-size: 18px;
	border-bottom: 1px solid #D1D1D1;
}
.mask-content .check-p{
	height: 66px;
	line-height: 66px;
	position: absolute;
	bottom: 0px;
	left: 0px;
	width: 100%;
}
.mask-content .check-p span{
	width: 49%;
	display:inline-block;
	text-align: center;
	color:#d4361d ;
	font-size: 18px;
}
.check-p .del-com{
	border-right: 1px solid #D1D1D1;
}

这是我在看到别人写的jQuery带删除功能多图片上传预览插件根据自己的需求进行了修改,主要内容是多图上传,点击加图片可以叠加上传,修改时只会上传新增的图片,删除图片是采用单张单张删除,有需要的可以进行参考。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值