我是 React Native 和 Strapi 的新手,我想将图片上传到 API:我的应用程序允许用户添加销售广告(标题、描述、数量、手数、价格、照片 1、照片 2、.. .)。
我可以添加带有文本字段但不能添加照片的促销广告。而且我在 Strapi 的文档中找不到如何做(我使用 MongoDB)。知道 Strapi 不接受 FormData。目前,我以“base64”格式获取智能手机的图片并将其发送到 API,但它不起作用。
Insert_Into_DataBase = () => {
console.log("insert");
console.log(this.state.image1);
this.setState(
{
ActivityIndicator_Loading: true
},
() => {
console.log("fetch");
fetch("http://192.168.0.102:1337/annonces", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
titreAnnonce: this.state.titre,
description: this.state.description,
qte: this.state.disponibilite,
nbrLots: this.state.lots,
Image1: this.state.image1
})
})
.then(response => response.json())
.then(responseJsonFromServer => {
alert(responseJsonFromServer + "L'annonce à bien été postée");
this.setState({ ActivityIndicator_Loading: false });
})
.catch(error => {
console.error(error);
this.setState({ ActivityIndicator_Loading: false });
});
}
);
};