我想用http.post发送图片。我使用该代码块,但给出了404错误。我在邮递员身上试过api,回归成功。
我哪里错了?
感谢。
changeprofileimg()
{
this.camera.getPicture({
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.DATA_URL,
quality: 100,
encodingType: this.camera.EncodingType.PNG,
targetHeight:500,
targetWidth:500,
correctOrientation:true
}).then(imageData => {
const image = 'data:image/jpeg;base64,'+imageData;
var veri;
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization' , 'Bearer '+this.globalvars.getToken());
headers.append('Content-Type', 'multipart/form-data');
let options = new RequestOptions({ headers: headers });
let postParams = {
file:image
}
this.http.post("http://.../api/profile/image/upload", postParams, options)
.subscribe(data => {
console.log( data['_body']);
veri=data['_body'];
veri = veri.replace(/\\/g, "");
veri = JSON.parse(veri);
console.log(veri);
});
});
}
答案 0 :(得分:2)
您应该使用FormData来实现此目的:
let formData = new FormData();
formData.append('file', image);
this.http.post("http://.../api/profile/image/upload", formData, options)
.subscribe(data => {
console.log( data['_body']);
veri=data['_body'];
veri = veri.replace(/\\/g, "");
veri = JSON.parse(veri);
console.log(veri);
});
});
除此之外,当你收到404错误时,你应该仔细检查你的api端点和其他api方法。