声明:代码仅供参考,只展示关键部分代码
HTML部分代码
<div class="layui-form" style="margin-left: 20px;margin-top: 10px;">
<div class="layui-form-item">
<label class="layui-form-label">参数1:</label>
<div class="layui-input-inline">
<input type="text" id="" lay-verify="required" autocomplete="off" class="layui-input">
</div>
<div style="position:relative;" class="layui-btn hand" style="width: 30px;" id="uploadDiv">
上传附件
<input type="file" style="opacity: 0;width: 100px;height: 38px;" name="uploadFile" id="upload" onchange="setName()" />
</div>
<span id="" style="display:inline-block;width:120px;white-space:nowrap;text-overflow:ellipsis;overflow:hidden;" title=""></span>
<button class="layui-btn" style="width: 150px;position: absolute;margin-left: 20px;" lay-submit lay-filter="formSubmit">添加</button>
</div>
</div>
后台接收文件以及参数接口
@PostMapping("addLine")
public Result addLine(Line line, MultipartFile file){
System.out.println(line);
Result result = new Result();
try {
Integer num = service.addLine(line,file);
if(num>0) {
result.addOK("添加成功");
}else if(num<0){
result.addError("添加失败");
}else{
result.addError("行名称重复");
}
}catch (Exception e){
result.addError("添加失败");
logger.error("添加失败",e);
}
return result;
}
前台传递需要上传的文件以及参数
var file = document.getElementById("upload").files[0];
var formData = new FormData();
formData.append("file",file);
formData.append("reviewId", reviewId);
$.ajax({
url: rowAddUrl + "/addLine",
type: "POST",
data: formData,
dataType: "json",
contentType: false,
processData : false,
success: function(result) {
if (result.status != "OK") {
layer.msg(result.message, {
icon: 5
});
return;
}
layer.msg(result.message, {
icon: 6
});
clear();
getData();
},
error: function() {
layer.msg("发送请求失败!", {
icon: 5
})
}
});