Html部分
<form id="form" method="post">
a:<input type="text" v-model="a" name="acc">
<input type="file" name="file" id="file">
<input type="button" id="button" @click="btn">
</form>
js部分
function btn() {
var file = document.getElementById("file").files[0]
var formData = new FormData(document.getElementById("form"))
// formData.append("file",file)
// formData.append("name","name")
alert(1)
axios({
method:"POST",
url:"/sys/test/test",
processData:false,
contentType:false,
data:formData,
ntentType : false,
}).then((e)=>{
console.log(e);
})
}
后端代码
@PostMapping("/test")
@ResponseBody
public Result test(@RequestPart("file") MultipartFile file,String acc){
System.out.println(acc);
// System.out.println(json);
// MultipartFile file = (MultipartFile) json.get("file");
String originalFilename = file.getOriginalFilename();
System.out.println(originalFilename);
Result result = new Result();
result.setCode("001");
System.out.println(1);
return result;
}
后端代码参数中要与form表单里面的name属性的值相对应