后台部分
@PostMapping("ocr/upload/file")
public ApiRestResponse upload(@RequestParam("file") MultipartFile file) {
controller是这样的
前台用axios发送post请求上传图片
async uploadFile() {
const formData = new FormData()
// 这里的image要对应controller的@RequestParam 参数名
formData.append('image', this.file)
try {
const response = await axios.post('http://localhost:8000/ocr/upload/file', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
console.log(response);
this.imageUrl = response.data.url
} catch (error) {
console.error(error)
}
}
然后理所当然的会报错
Required request part 'file' is not present
花了30分钟看各种帖子,无果。我这里的问题是把上面的 formData.append('image', this.file)
改成 formData.append('file', this.file) 就行,和上面controller 的@RequestParam("file") 名字一样就可以了