js多图上传

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>HTML多图上传</title>
        <style>
            body {
                background: #edf0f2;
            }

            img {
                width: 100%;
                display: block;
                border: none;
                vertical-align: bottom;
                border: none;
            }

            input[type="button"] {
                -webkit-appearance: none;
            }

            .productDrawingBox {
                background-color: #fcfcfc;
                color: #333333;
                font-size: 16px;
                padding-left: 11px;
                border-bottom: solid 1px #e5e5e5;
            }

            .productDescription {
                height: 44px;
                line-height: 44px;
            }

            .productImg {
                height: 96px;
                overflow: hidden;
            }

            .imgBiMG {
                width: 78px;
                height: 81px;
                float: left;
                display: block;
            }

            .uploadDIv {
                width: 78px;
                height: 81px;
                background-color: #edf0f2;
                font-size: 28px;
                color: #bfbfbf;
                text-align: center;
                line-height: 81px;
                float: left;
                position: relative;
            }

            .uploadDIv input {
                width: 78px;
                height: 78px;
                opacity: 0;
                position: absolute;
                right: 0px;
                top: 0px;
                z-index: 4;
                padding: 0;
            }
        </style>
        <script src="js/jquery-3.6.3.js"></script>
    </head>
    <body>
        <div class="productDrawingBox">
            <div class="productDescription">上传图片</div>
            <div class="productImg">
                <div id="uploadBox">
                </div>
                <div class="uploadDIv">
                    <span>+</span><input type="file" name="file" multiple id="inputs" accept="image/*" class='fileTest'
                        multiple="multiple" />
                </div>
            </div>
        </div>
        
        <script type="text/javascript">
            $(function() {
                var img = []; //创建一个空对象用来保存传入的图片
                var AllowImgFileSize = '101376'; //1兆
                $("#inputs").change(function() {
                    var fil = this.files;
                    for (var i = 0; i < fil.length; i++) {
                        var curr = $('#inputs')[i].files[0].size;
                        if (curr > AllowImgFileSize * 101376) { //当图片大于1兆提示
                            layer.msg("图片文件大小超过限制 请上传小于99M的文件");
                        } else {
                            reads(fil[i]);
                            img.push($('#inputs')[i].files[0]); //将传入的图片push到空对象中
                        }
                    }
                    if (img.length >= 6) { //判断图片的数量,数量不能超过3张
                        $('.uploadDIv').hide();
                    } else {
                        $('.uploadDIv').show();
                    }
                    console.log(img);
                });
        
                function reads(fil) {
                    var reader = new FileReader();
                    reader.readAsDataURL(fil);
                    reader.onload = function() {
                        document.getElementById("uploadBox").innerHTML +=
                            "<div class='divImg' id='uploadImg'><img src='" + reader.result +
                            "' class='imgBiMG'></div>";
                    }
                }
            })
        </script>
    </body>
    
</html>

标注此功能只使用于页面传输,在接口中运用会有bug,读者请根据情况使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值