【发布时间】:2018-03-08 10:54:20
【问题描述】:
我正在使用http.post 提交包含title 和description 等字段的表单,它工作正常。我还允许用户使用相机拍摄照片并将其保存为base64 格式的字符串。我需要通过同一个 POST 请求将这张照片提交给服务器。我怎样才能做到这一点?到目前为止,我的代码如下,服务器在名为“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));
}
);
【问题讨论】:
标签: ionic-framework ionic2 angular2-forms angular-file-upload