I am submitting a form with fields like title
and description
using http.post
and it works fine. I also allow user to use the camera to capture a photo and save it as a string in base64
format. I need to submit this photo to the server via the same POST request. How can I do this? My code so far is as the following and the server looks for the uploaded photo in a field named "photo":
headers = new Headers({'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'});
options = new RequestOptions({ headers: this.headers });
let data = {
title: item.title,
description: item.description
};
let params = new URLSearchParams();
for(let key in data){
params.set(key, data[key])
}
this.http.post('http://example.com/items', params.toString(), this.options).subscribe(
(result) => {
console.log("success!");
},
(err) => {
console.log(JSON.stringify(err));
}
);