layui实现文件上传+表单一起上传

$(document).ready(function() {
    $('#step-backward').click(function(){location = '/terminal/apk'});

    //序列化表单
    $.fn.extend({
        serializeObject : function() {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if(this.name != "file"){//排除文件上传
                    if (o[this.name]) {
                        if (!o[this.name].push) {
                            o[this.name] = [ o[this.name] ];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                }
            });
            return o;
        }
    });

    //上传文件
    layui.use('upload', function(){
        var upload =  layui.upload;    //apkUpload

        //apk文件上传
        var uploadInst = upload.render({
            elem: '#apkSelect'
            ,url: '/terminal/apk/do-add'    //上传接口
            ,auto: false
            ,bindAction: '#apkSubmit'
            ,accept:'file'
            ,size: 10240 //限制文件大小,单位 KB
            ,exts:'apk'
            ,multiple: false
            ,before: function(obj){ //obj参数包含的信息
                this.data= $('#apk_add_form').serializeObject();
            }
            ,done: function(data, index, upload){
                if (data.success) {
                    layer.msg("上传成功", {
                        icon : 6
                    });
                    window.location.href = "/terminal/apk";
                } else {
                    layer.msg("上传失败", {
                        icon : 5
                    });
                }

            }
        });
    });
    
})

后台

@PostMapping(value = { "/do-add" })
@ResponseBody
public Object doAdd(@RequestParam(required=false)MultipartFile file, ApkBO apkBO)

你可能感兴趣的:(Layui)