[英]How to make a post request with a image file using form-data in ionic-3
我的后端是一個簡單的Laravel API ,當我使用PostMan應用程序時,我的API可以正常工作。
我使用表單數據來發布沒有任何標題的請求。 它是100%有效的,但是當我嘗試使用離子代碼時卻無法正常工作
-這是錯誤-
selectphoto(){
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
saveToPhotoAlbum: false }
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL): base64Image = 'data:image/jpeg;base64,' + imageData;
this.image = normalizeURL(imageData);
}, (err) => {
// Handle error
});}
onSubmit(form:NgForm){
console.log('img' , this.image);
this.actorBestMovie = form.value.actorBestMovie;
this.actorCountry = form.value.actorCountry;
this.actorFirstMovie = form.value.actorFirstMovie;
this.actorImdbBestMovie = form.value.actorImdbBestMovie;
this.actorName = form.value.actorName;
this.actorpost();
form.reset()
}
actorpost() {
let headers = new Headers();
headers.append("Accept", "application/json");
let body = new FormData();
body.append('file',this.image);
body.append('name',this.actorName);
body.append('country',this.actorCountry );
body.append('first_movie',this.actorFirstMovie);
body.append('best_movie',this.actorBestMovie);
body.append('imdb_best_movie',this.actorImdbBestMovie);
this.http.post("http://localhost/slreview-api/public/api/actors",body,{headers: headers })
.map(res => res.json())
.subscribe(
data => {
console.log("data => ", data);
},
error => {
console.log('Error => ', error);
},
() => {
console.log('Completed!');
this.presentToast();
}
);}
我想發布我的數據,盡管發布請求謝謝!
更改參數的附加順序,並將圖像文件作為最后一個參數附加到FormData
如下所示。
let body = new FormData();
body.append('name',this.actorName);
body.append('country',this.actorCountry );
body.append('first_movie',this.actorFirstMovie);
body.append('best_movie',this.actorBestMovie);
body.append('imdb_best_movie',this.actorImdbBestMovie);
body.append('file',this.image);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.