typescript - Ionic3 多部分表单图片上传
问题描述
我想将多部分表单数据上传到restapi,当邮递员客户端请求它时,该api工作正常。
这是我的代码:
takePhoto() {
const options: CameraOptions = {
quality: 100,
allowEdit: true,
targetWidth: 200,
targetHeight: 200,
saveToPhotoAlbum: true,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
};
this.camera.getPicture(options).then(image => {
console.log('Image URI: ' + image);
this.image_File = image;
this.imgfile = this.imageURI;
console.log(JSON.stringify(this.imageURI));
this.avatar = image.slice(7);
this.imgfile = this.avatar;
}, error => {
console.log('Error: ' + error);
});
}
为了上传文件,我定义了一个函数:
console.log("begin to upload ");
let link = rootUrl+"merchant/merchant-image";
let data = new FormData();
// var blob = new Blob([this.image_File], { type: "image/jpg" });
data.append('merchantId', '2');
data.append('merchantImage',this.imgfile);
console.log(data);
this.http.post(link, data,{
headers:{
"Accept-Language":this.lang,
//'enctype': 'multipart/form-data',
// 'Content-Type': 'multipart/form-data',
}
}).subscribe(data2 => {
console.log("data Successful "+ data2);
}, error => {
console.log("Oooops! error "+ JSON.stringify(error));
});
来自服务器的响应:
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null
},
"status": 400,
"statusText": "OK",
"url": "http://xxxxxxxxxxx/merchant/merchant-image",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for http://xxxxxxxxxxxxx/merchant/merchant-image: 400 OK",
"error": {
"timestamp": "2018-08-14T10:13:14.693+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request part 'merchantImage' is not present",
"path": "/api/v1/merchant/merchant-image"
}
}
解决方案
推荐阅读
- python - 我希望 Python 处理目录中的所有 xml 文件,但它只处理最上面的文件
- python - Pandas 如何将字符串数组($0.00 格式)转换为小数
- opencl - 在不与 OpenCL 同步的情况下写入全局缓冲区是否可行?
- r - R中的联合()函数
- ios - Cocoapods:pod lint 有效,但由于缺少 versioned_frameworks,pod repo push 无法验证
- c# - 在 MVC 中为 ViewBag 分配值时无法执行运行时绑定到空引用
- performance - 预加载字体以加快网站速度,不要下载两次?
- c# - 从对象列表中删除重复项 c#
- python - 启动 Virtual Machine Manager 时出现超时错误
- python - Problems with a custom loss function in Keras