赞
踩
如题,即在springboot项目中的restfull接口,参数使用多个RequestPart的解决方案。
前端代码
<html> <head> <script> function onSubmit() { var formData = new FormData(); formData.append("file", document.forms["userForm"].file.files[0]); formData.append('user', new Blob([JSON.stringify({ "firstName": document.getElementById("firstName").value, "lastName": document.getElementById("lastName").value })], { type: "application/json" })); var boundary = Math.random().toString().substr(2); fetch('/upload', { method: 'post', body: formData }).then(function (response) { if (response.status !== 200) { alert("There was an error!"); } else { alert("Request successful"); } }).catch(function (err) { alert("There was an error!"); });; } </script> </head> <body> <form name="userForm"> <label> File : </label> <br/> <input name="file" type="file"> <br/> <label> First Name : </label> <br/> <input id="firstName" name="firstName" /> <br/> <label> Last Name : </label> <br/> <input id="lastName" name="lastName" /> <br/> <input type="button" value="Submit" id="submit" onclick="onSubmit(); return false;" /> </form> </body> </html>
后端代码
@RestController
@RequestMapping
public class UploadController {
@PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseData<SubmitDocResult> submit(@RequestPart("user") User user, @RequestPart(value = "file") MultipartFile file) {
//TODO
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。