gpt4 book ai didi

angular - 如何使用 ionic-3 中的表单数据对图像文件发出发布请求

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:39 30 4
gpt4 key购买 nike

我的后端是一个简单的 Laravel API,当我使用 PostMan 应用程序时,我的 API 工作正常。

我使用表单数据来发布没有任何标题的请求。它是 100% 工作但是当我尝试我的 ionic 代码时它不工作 This is postman request

-- 这是错误--

this is the error massage

---这是我在第 55 行显示的后端代码错误: enter image description here

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);

关于angular - 如何使用 ionic-3 中的表单数据对图像文件发出发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54258095/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com