Uploading file with other form fields in Angular 2 and Spring MVC

前端 未结 3 1246
逝去的感伤
逝去的感伤 2020-12-30 15:26

I am trying to upload file and other form field contents from my Angular 2 front end to Spring back end. But somehow I am not able to do it. Here is my code

3条回答
  •  庸人自扰
    2020-12-30 16:05

    You need to use @RequestPart instead of @RequestParam and set consumes attribute:

    @RequestMapping(value = "/file",method = RequestMethod.POST, consumes = "multipart/form-data")
    @ResponseBody
    public ResponseEntity addUser(@RequestPart("uploadFile") MultipartFile file, @RequestPart("info") ExampleModel model){}
    

    You also need to adjust your FormData object:

    formData.append('uploadFile', file, file.name);
    formData.append('info', new Blob([JSON.stringify(this.model)],
            {
                type: "application/json"
            }));
    

提交回复
热议问题