Save FormData with File Upload in Angular 5

前端 未结 5 839
鱼传尺愫
鱼传尺愫 2021-01-01 00:49

I am trying to save files along with FormData in Angular 5. I can get the single file, but have no idea how to get all the files uploaded. I have three image files and input

5条回答
  •  隐瞒了意图╮
    2021-01-01 01:37

    I recently had a similar issue. This can be solved by setting the content type of header as null in the Angular code. Attaching the code snippet of Angular5 and spring boot backend.

    let headers = new HttpHeaders();
    //this is the important step. You need to set content type as null
    headers.set('Content-Type', null);
    headers.set('Accept', "multipart/form-data");
    let params = new HttpParams();
    const formData: FormData = new FormData();
    for (let i = 0; i < this.filesList.length; i++) {
      formData.append('fileArray', this.filesList[i], this.filesList[i].name);
    } 
    formData.append('param1', this.param1);
    formData.append('param2', this.param2);
    this.http.post(this.ROOT_URL + this.SERVICE_ENDPOINT, formData, { params, headers }).subscribe((res) => {
        console.log(res);
    });
    
    
    
    In the spring boot backend, you need to have the controller as - 
    
    @RequestMapping(value = "/uploadAndSendEmail", method = RequestMethod.POST, consumes= "multipart/form-data")    
    public ResponseEntity uploadAndSendEmail(@RequestParam("fileArray") MultipartFile[] fileArray, 
            @RequestParam(value = "param1", required = false) String param1,
            @RequestParam(value = "param2", required = false) String param2) {
            //do your logic
            }
    

提交回复
热议问题