How to send multipart file from angular 4 using http

Viewed 817

I need to send a file using multipar file which my spring controller expects.

public void test(@RequestParam("file") MultipartFile file) {}

I know that I have to use FormData, but I did not manage to make it work. Here's my code:

this.headers = new Headers({ 'Content-Type': 'multipart/form-data'});
this.options = new RequestOptions({ headers: this.headers });

return this.http.post(environment.SERVER_ENDPOINT + 'parameters/area', formData, options)
  .toPromise()
  .then(this.extractData)
  .catch(this.handleError);
1 Answers

Use XMLHttpRequest

let fd = new FormData();
fd.append("file", file);
let xhr = new XMLHttpRequest();
const url = environment.SERVER_ENDPOINT + 'parameters/area';
xhr.open('POST', url, true);
xhr.send(fd);
Related