如何将图像选择器中的图像添加到离子2中的表单数据发布请求?
我知道api正在运行,因为我可以使用邮递员将图像发送给它。
这是我写的,但它不起作用:
// this.image is Promise<any>
let options = {
maximumImagesCount: 1
};
this.imagePicker.getPictures(options).then((results) => {
this.image = results[0];
this.toastCtrl.create({
message: "Image Selected",
duration: 3000,
position: 'top'
}).present();
},
(err) => {
this.toastCtrl.create({
message: err,
duration: 3000,
position: 'top'
}).present();
});
headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let body = new FormData();
body.append("image", this.image);
this.http.post('api', body, new RequestOptions({ headers: headers }))
.map(res => res.json())
.subscribe(data => {
this.toastCtrl.create({
message: data,
duration: 3000,
position: 'top'
}).present();
});