Peter Penzov
Peter Penzov

Reputation: 1626

Send additional param for file upload

I want to implement file upload from Angular to Spring with one additional param into the request: I tried this:

upload_logo(){
this.merchantService.imports(this.fileToRead, this.merchant.id)
    .subscribe(res => {
        console.log(res);
    });
}

  imports (file: any, id: number) {
    const formData = new FormData();
    formData.append('file', file, file.name);
    return this.http.post(environment.api.urls.merchants.uploadLogo, formData, id);

}

merchants: {
   uploadLogo: baseUrl + '/api_admin/merchants/upload'
}

BE:

@PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<StringResponseDTO> uploadData(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes, @RequestParam("id") Integer number) throws Exception {
    ....
}

But I get error here:

return this.http.post(environment.api.urls.merchants.uploadLogo, formData, id);

How I need to pass the id number to the POST request?

Upvotes: 0

Views: 417

Answers (1)

Buczkowski
Buczkowski

Reputation: 2416

this.http.post(environment.api.urls.merchants.uploadLogo, formData, {params: {id: id.toString()}});

I added toString because params object should have key as string and value as string or array of strings.

https://angular.io/api/common/http/HttpClient#post

Upvotes: 1

Related Questions