0

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?

1 Answer 1

1
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

Sign up to request clarification or add additional context in comments.

3 Comments

I want to send a number. Are you sure that toString can be sued for number?
/some/url?param=24 - how you distinguish param value whenever it is an number or string? Provide it as string here and on backend parse to number. Eventually you can pass it to body and you'll get it as number but I don't think it's a good idea.
Can you show me how to send it as body param not URL param?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.