前端
<!DOCTYPE html>
<html lang="en" class="no-js" xmlns:th="http://www.thymeleaf.org">
<body id="body" data-spy="scroll" data-target=".header">
<!--<div th:replace="theme/left-music::left-music"></div>-->
<!-- Products -->
<div>
<input id="name" />
<input type="file" id="file" />
<button id="btnShow" class="button">提交</button>
</div>
<script th:src="@{/diy/vendor/jquery.min.js}" type="text/javascript"></script>
<script>
$(function () {
$("#btnShow").on('click', function () {
debugger
var formdata = new FormData();
formdata.append("name", $("#name").val());
formdata.append("file", $("#file")[0].files[0]);
$.ajax({
type: 'POST',//方法类型
url: "/test/upload",
data: formdata,
cache: false,
processData: false,
contentType: false,
success: function (result) {
console.log(result);
},
error: function () {
swal("操作失败", {
icon: "error",
});
}
});
});
})
</script>
</body>
<!-- END BODY -->
</html>
后端
/**
*
* @return
*/
@PostMapping({"/test/upload"})
public String test(TestData testData) {
System.out.println(testData);
return "success";
}
private static class TestData{
private String name;
private MultipartFile file;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}