0

我是 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 });
      });
  }
);
   };
4

2 回答 2

4

实际上,要将上传添加到条目(announces在您的情况下),您必须首先创建您的条目。

然后您将不得不使用该/upload路由来上传您的文件(一个一个)。这里有关于参数设置的文档以将文件链接到正确的条目https://strapi.io/documentation/guides/upload.html#usage

之后,如果您对文件格式有疑问,可以在此处查看https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js#L34文件格式转换后,您可能会更新 id 来处理base64文件。

于 2018-08-26T12:28:42.460 回答
0

Strapi 现在允许我们发送表单数据以进行文件上传。创建实体和上传文件可以在一个发布请求中完成。请参考:https ://strapi.io/documentation/developer-docs/latest/development/plugins/upload.html

于 2021-03-30T11:39:43.970 回答