Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required req
·
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]
这几天想整个Minio玩玩,前后端上传文件时爆了这个警告
后端代码:
@Transactional
@PostMapping("/upload")
public Result<String> upload(@RequestParam("file") MultipartFile multipartFile){
result Result.ok();
}
前端代码:
uploadFileButton.addEventListener("click", () => {
const file = document.querySelector('#file').files[0];
const data = new FormData().append('file', file);
axios({
method: 'post',
url: 'http://localhost:8080/upload',
data: {data},
headers: {
'Content-Type': 'multipart/form-data'
}
})
})
因为是SprinBoot找不到对应的参数,所以可以确定是前端的问题,然后发现了两个问题:
- new FormData()不能直接接.append(),否则返回数据应该不是FormData对象
- axios传参时不能携带{},否则识别不出来
如果是前端没问题,一般需要检查下@RequestParam("file")指定的参数名是否和append('file', file)指定的参数名一致,或者检查下配置文件上传文件大小
修改后的前端代码:
uploadFileButton.addEventListener("click", () => {
const file = document.querySelector('#file').files[0]
const data = new FormData();
data.append('file', file);
axios({
method: 'post',
url: 'http://localhost:8080/upload',
data: data,
headers: {
'Content-Type': 'multipart/form-data'
}
})
})
数据后端可正常接收
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)