axios上传文件到SpringBoot后台出现的问题

后台部分

    @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") 名字一样就可以了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我来为你回答这个问题。 首先,在Vue项目中引入axios需要先安装axios,可以使用以下命令: ``` npm install axios --save ``` 然后在需要使用axios的Vue组件中引入axios: ``` import axios from 'axios' ``` 接下来,我们可以使用axios后台发送请求,以下是一个示例: ``` axios.get('/api/users').then(response => { console.log(response.data) }) ``` 上述代码向后台发送了一个GET请求,请求的路径是`/api/users`,并在请求成功后打印了响应数据。 接下来,我们需要后台提供相应的接口来完成增删改查操作。这里以Spring Boot为例,我们可以使用Spring Boot提供的注解来定义RESTful API接口。以下是一个示例: ``` @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getUsers() { return userService.getUsers(); } @PostMapping public void addUser(@RequestBody User user) { userService.addUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable Long id, @RequestBody User user) { userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } ``` 上述代码中,我们定义了一个`UserController`类,该类提供了四个接口,分别对应了获取用户列表、添加用户、更新用户和删除用户四个操作。其中,`@GetMapping`、`@PostMapping`、`@PutMapping`和`@DeleteMapping`分别对应了HTTP协议中的GET、POST、PUT和DELETE请求。 最后,我们可以在Vue项目中使用axios来调用这些接口,以完成增删改查操作。以下是一个示例: ``` // 获取用户列表 axios.get('/api/users').then(response => { console.log(response.data) }) // 添加用户 axios.post('/api/users', { name: '张三', age: 18, gender: '男' }).then(response => { console.log(response.data) }) // 更新用户 axios.put('/api/users/1', { name: '李四', age: 20, gender: '女' }).then(response => { console.log(response.data) }) // 删除用户 axios.delete('/api/users/1').then(response => { console.log(response.data) }) ``` 上述代码分别调用了四个接口,以完成增删改查操作。其中,`axios.post`、`axios.put`和`axios.delete`中的第二个参数分别为要添加、更新和删除的用户数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值