java关于form表单的序列化提交(包括带有file)

1.普通 form表单序列化提交

1):

var o = {};
    var a = $('#form').serializeArray();//序列化为array  
    alert(a);//转json格式
    alert(JSON.stringify(a));//转json格式
    //将序列化陈的array  然后转化为 ajax格式的json  数据
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    alert(JSON.stringify(o));//转json格式1)  


2):

$("#form").data("bootstrapValidator").validate();
            if (!$("#form").data("bootstrapValidator").isValid()) {
                return;
            } else{
                $.ajax({
                    url : createUrl('familyFileNum/fileNum/addfilNum'),
                    type: 'POST',
                    data:$('#form').serialize(),
                    datatype:'json',
                    success : function(msg){
                        if(msg.success){
                            layer.alert(msg.msg,{icon:1},function(){
                                parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));
                                parent.layer.closeAll();
                            });
                        }else{
                            layer.alert(msg.msg,{icon:2});
                        }
                    },
                    error : function(msg){
                        layer.alert("系统发生错误,请稍后再试",{icon:5});
                    }
                });
            }


2.带文件form 序列化

1):

var form = new FormData(document.getElementById("form"));
    $.ajax({
        url : createUrl('familyRegister/addRegister'),
        data : form,
        type : 'POST',
        processData : false,
        contentType : false,
        success : function(msg) {
            if (msg.success) {
                layer.alert(msg.msg, {
                    icon : 1
                }, function() {
                    parent.layer.closeAll();
                    if (parent.$(".in.active").find("iframe").size()) {
                        parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));
                    } else {
                        parent.window.location.reload();
                    }
                });
            } else {
                layer.alert(msg.msg, {
                    icon : 2
                });
            }
        },
        error : function(msg) {
            layer.alert("系统发生错误,请重新登录或者稍后再试", {
                icon : 5
            });
        }
    });




2):

$('#form').ajaxSubmit({
                success : function(msg){
                    if(msg.success){
                        layer.alert(msg.msg,{icon : 1},function(){
                            parent.$(".in.active").find("iframe").attr('src', parent.$(".in.active").find("iframe").attr('src'));
                            parent.layer.closeAll();
                        });
                    }else{
                        layer.alert(msg.msg,{icon:2});
                    }
                },
                error : function(msg){
                    layer.alert("系统发生错误,请重新登录或者稍后再试",{icon:5});
                }
            });



要实现jqueryform表单提交下载文件,可以采用以下步骤: 1. 在前端页面中添一个表单,用于提交下载文件的请求。 2. 使用jqueryform插件将表单序列化并发送到后台。 3. 后台接收到请求后,根据表单中的参数生成需要下载的文件,并将文件保存到服务器端。 4. 后台将生成的文件路径返回给前端。 5. 前端使用window.open()方法打开返回的文件路径即可实现文件下载。 以下是示例代码: 前端代码: ``` <form id="downloadForm" action="/downloadFile" method="post"> <input type="hidden" name="fileName" value="example.txt"> <input type="hidden" name="fileContent" value="This is an example file."> <input type="submit" value="Download"> </form> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js"></script> <script> $(document).ready(function(){ $("#downloadForm").submit(function(e){ e.preventDefault(); $(this).ajaxSubmit({ success: function(data){ window.open(data); } }); }); }); </script> ``` 后台代码: ``` @RequestMapping(value = "/downloadFile", method = RequestMethod.POST) @ResponseBody public String downloadFile(@RequestParam("fileName") String fileName, @RequestParam("fileContent") String fileContent, HttpServletRequest request, HttpServletResponse response) throws Exception { // 生成文件并保存到服务器端 String filePath = "/path/to/file/" + fileName; File file = new File(filePath); FileWriter writer = new FileWriter(file); writer.write(fileContent); writer.flush(); writer.close(); // 返回文件路径 return filePath; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值