js控制只能上传‘jpg‘, ‘png‘, ‘bmp‘, ‘jpeg‘, ‘gif‘格式文件

js控制只能上传’jpg’, ‘png’, ‘bmp’, ‘jpeg’, 'gif’格式文件,且最多只能长传2张图片,两张图片文件名中间用逗号分隔

效果图:

在这里插入图片描述

html:

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('通过原因')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="gray-bg">

<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-examinerecord-examine-id" th:object="${rankingExamineRecord}">
        <input name="id" id="id" th:field="*{id}" hidden>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">审核原因:</label>
            <div class="col-sm-8">
                <textarea name="opinion" id="opinion" maxlength="300" class="form-control" placeholder="请输入审核原因,不超过300字" required></textarea>
            </div>
        </div>
        <input name="fileid" id="fileid" value="0" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">上传模板附件:</label>
            <div class="col-sm-8">
                <div class="file-loading">
                    <input class="form-control file-upload" type="file" name="file" id="file" multiple>
                </div>
            </div>
        </div>
        <div class="form-group" style="margin-left: 40%;margin-top: 40px">
            <div class="col-sm-8">
                <button type="button" id="pass" class="btn btn-sm btn-success" onclick="passById()">通过</button>
                <button type="button" class="btn btn-sm " onclick="closeLayer()">关闭</button>
            </div>
        </div>
    </form>
</div>

<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "erecord/recommendation";

    function passById(index, layero) {
        if($("#fileid").val()=='0'){
            $.modal.alertError("请上传模板附件后再进行操作!")
        }else{
            var id=$("#id").val();
            if ($.validate.form("form-examinerecord-examine-id")) {
                $.ajax({
                    type: "post",
                    url: prefix + "/cityExamineSuccessById/" + id,
                    async: true,
                    data: {
                        "opinion":$("#opinion").val(),
                        "fileid":$("#fileid").val()
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            $.modal.alertSuccess(result.msg);
                            $.modal.close();
                        } else {
                            $.modal.alertError(result.msg)
                        }
                    },
                    error: function (error) {
                        $.modal.alertError(error.msg);
                    }
                });
            }
        }

    }

    //关闭leyer弹出层
    function closeLayer() {
        $.modal.close();
    }

    $(".file-upload").fileinput({
        uploadUrl: ctx+"fastdfs/uploadToEnt",
        allowedFileExtensions: ['jpg', 'png', 'bmp', 'jpeg', 'gif'],
        dropZoneTitle: '可以将文件拖放到这里,支持文件上传',
        maxFileSize: 0,
        maxFileCount: 2,
        showType: 'edit',
        layoutTemplates:{
            actionDelete:''
        }
    });
    var arrayObj = new Array();
    $(".file-upload").on("fileuploaded", function (event, data, previewId, index) {
        var obj = data.response;
        $("#file").removeAttr('required');

        arrayObj.push(data.response.fileid);
        if(arrayObj.length==2){
        	//两张图片文件名中间用逗号分隔
            var fileids=arrayObj[0]+','+arrayObj[1];
            $("#fileid").val(fileids);
            $(this).parents(".input-group").css('display', 'none');//上传按钮隐藏
        }else{
            $("#fileid").val(data.response.fileid);
        }
    });

    $(".file-upload").on('filesuccessremove', function(event, id) {
        $(this).parents(".input-group").css('display', 'block');
        $('#file').attr('required',"true");
        $("#fileid").val('0');
    });

</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值