单文件上传和多文件上传

若依项目下jasny-bootstrap-js插件

引入css

<th:block th:include=“include :: jasny-bootstrap-css”/>

引入js

<th:block th:include=“include :: jasny-bootstrap-js”/>

单文件上传

html部分
<div class="form-group">
    <label class="col-sm-2 control-label">附件上传:</label>
    <div class="col-sm-10">
        <div class="fileinput fileinput-new input-group" data-provides="fileinput" data-url="/common/upload">
            <div class="form-control" data-trigger="fileinput" style="line-height: 15px;"> <!--让图标文字居中-->
            	<i class="glyphicon glyphicon-file fileinput-exists"></i> 
            	<span class="fileinput-filename"></span> <!--文件名显示-->
            </div>
            <span class="input-group-addon btn btn-white btn-file">
            <span class="fileinput-new">选择文件</span>
            <span class="fileinput-exists">更改</span>
            <input type="file" accept=".pdf,.xlsx,.xls,.doc,.docx"
                   onchange="validateFile(this)">
            </span>
            <a href="javascript:;" class="input-group-addon btn btn-white fileinput-exists"
               data-dismiss="fileinput">清除</a>
        </div>
        <p style="margin-top: 15px;">
            <small class="form-text text-muted" style="font-size: 14px;">只能上传PDF、Excel和Word文件,单个文件不超过5MB。</small>
        </p>
    </div>
</div>
js部分
function validateFile(input) {
    var file = input.files[0];
    var maxSize = 5 * 1024 * 1024; // 5MB
    var formData = new FormData();

    var fileSize = file.size;
    formData.append('file', file); // 将文件添加到 FormData 对象中

    if (fileSize > maxSize) {
        alert('文件 "' + file.name + '" 大小超过限制,单个文件不超过5MB。');
        input.value = ''; // 清空文件输入框中的内容
        return false;
    }

    var allowedExtensions = /(\.pdf|\.xls|\.xlsx|\.doc|\.docx)$/i; // 只允许PDF、Excel和Word文件
    if (!allowedExtensions.exec(file.name)) {
        alert('文件 "' + file.name + '" 类型不支持,只能上传PDF、Excel和Word文件。');
        input.value = ''; // 清空文件输入框中的内容
        return false;
    }

    $.ajax({
        url: '/common/upload',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
             if (response.code === 0){
                 $('input[name="filePath"]').val(response.fileName);
             }
        },
        error: function(xhr, status, error) {
            console.error('Error uploading file:', error);
        }
    });

    return true;
}

多文件上传

html部分
<div class="form-group">
    <label class="col-sm-2 control-label">附件上传:</label>
    <div class="col-sm-10">
        <div class="fileinput fileinput-new input-group" data-provides="fileinput">
            <div class="form-control" data-trigger="fileinput" style="line-height: 15px;"> <!--让图标文字居中-->
            	<i class="glyphicon glyphicon-file fileinput-exists"></i> 
            	<span class="fileinput-filename"></span> <!--文件名显示-->
            </div>
            <span class="input-group-addon btn btn-white btn-file">
            <span class="fileinput-new">选择文件</span>
            <span class="fileinput-exists">更改</span>
            <input type="file" accept=".pdf,.xlsx,.xls,.doc,.docx" multiple
                   onchange="validateFiles(this)">
            </span>
            <input type="hidden" name="filePath">
            <a href="javascript:;" class="input-group-addon btn btn-white fileinput-exists"
               data-dismiss="fileinput">清除</a>
        </div>
        <p style="margin-top: 15px;">
            <small class="form-text text-muted" style="font-size: 14px;">只能上传PDF、Excel和Word文件,单个文件不超过5MB。</small>
        </p>
    </div>
</div>
js部分
function validateFiles(input) {
        var files = input.files;
        var maxSize = 5 * 1024 * 1024; // 5MB
        var formData = new FormData();

        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var fileSize = file.size;
            formData.append('files', files[i]); // 将每个文件添加到 FormData 对象中

            if (fileSize > maxSize) {
                alert('文件 "' + file.name + '" 大小超过限制,单个文件不超过5MB。');
                input.value = ''; // 清空文件输入框中的内容
                return false;
            }

            var allowedExtensions = /(\.pdf|\.xls|\.xlsx|\.doc|\.docx)$/i; // 只允许PDF、Excel和Word文件
            if (!allowedExtensions.exec(file.name)) {
                alert('文件 "' + file.name + '" 类型不支持,只能上传PDF、Excel和Word文件。');
                input.value = ''; // 清空文件输入框中的内容
                return false;
            }
        }

        $.ajax({
            url: '/common/uploads',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                 if (response.code === 0){
                     $('input[name="filePath"]').val(response.fileNames);
                 }
            },
            error: function(xhr, status, error) {
                console.error('Error uploading files:', error);
            }
        });

        return true;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值