Jquery Ajax上传文件并提交表单其它属性

198 阅读1分钟

1.前端代码

$('.btn-confirm').click(function () {
            console.log($( '#postForm').serialize());

            var versionNo = $("#versionNo").val();
            var deviceNoStr = $("input[name='deviceNoStr']").val();
            var batchId = $("input[name='batchId']").val();

            var formData = new FormData();
            console.log($('#upfile')[0].files[0]);

            formData.append("upfile",$('#upfile')[0].files[0]);
            formData.append("versionNo", versionNo);
            formData.append("deviceNoStr", deviceNoStr);
            formData.append("batchId", batchId);

            $.ajax({
                url :window.CMS_URL + "/devops/operation/upgrade.shtml",
                dataType:'json',
                type:'POST',
                async: false,
                data: formData,
                processData : false, // 使数据不做处理
                contentType : false, // 不要设置Content-Type请求头
                success: function(data){
                    console.log(data);
                    if (data.status == 'ok') {
                        alert('上传成功!');
                    }

                },
                error:function(response){
                    console.log(response);
                }
            });

        });

 

 

<form id= "uploadForm" method= "post" enctype ="multipart/form-data">
            <div class="portlet-body" style="margin: 15px 0;">
                <span class="file-des">程序路径:</span>
                <div class="wrap-upload" style="display: flex">
                    <div class="showFileName"
                    ></div>
                    <a href="javascript:;" class="file btn btn-sm green">上传文件
                        <input type="file" name="upfile" id="upfile" placeholder="请选择文件">
                    </a>
                    <div class="fileerrorTip" style="line-height: 30px;margin-right: 10px"></div>
                </div>
            </div>
            <div class="portlet-body">
                <span class="file-des">版本号:</span>
                <div class="wrap-upload item-inputs" >
                    <input class="form-control" type="text" name="versionNo" id="versionNo" placeholder="版本号" required>
                </div>
            </div>
            <div style="    margin: 0px 100px;padding-top: 20px;">
                <div class="btn btn-sm green control-btn-item btn-confirm" >确认</div>
                <div style="margin-left: 30px;" class="btn btn-sm green control-btn-item" onclick="closePopup()">取消</div>
            </div>

            <input type="hidden" name="deviceNoStr" value="${(devopsParamsQo.deviceNoStr)!''}"/>
            <input type="hidden" name="batchId" value="${(devopsParamsQo.batchId)!''}"/>
        </form>

 

2.后端代码

 @RequestMapping(value = "/upgrade", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo upgrade(@RequestPart( value = "upfile", required = true) MultipartFile file,
                            @RequestParam(name = "versionNo", required = true) String versionNo,
                            @RequestParam(name = "batchId", required = true) String batchId,
                            @RequestParam(name = "deviceNoStr", required = true) String deviceNoStr) {
        DevopsParamsQo qo = new DevopsParamsQo();
        qo.setDeviceNoStr(deviceNoStr);
        List<DeviceDto> addList = getDeviceDtos(qo);

        // TODO 调用前置
        return ResultVoGenerator.genSuccessResultVo();
    }