【问题标题】:Upload Image - Strapi - React Native上传图片 - Strapi - React Native
【发布时间】:2018-08-26 10:53:23
【问题描述】:

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

【问题讨论】:

    标签: react-native strapi


    【解决方案1】:

    实际上要将上传添加到条目(在您的情况下为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文件。

    【讨论】:

    • 如果我要上传 4 张照片,我必须这样做: - 发布 => 制作广告 - 发布 => 上传我的照片 1 - 发布 => 上传我的照片 2 - 发布 => 上传我的照片 3 - POST => 上传我的 photo4 - PUT => 更新我的广告的字段“photo1”、“photo2”、“photo3”、“photo4”。这是很多要求。我会试试的。谢谢!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 2017-06-29
      • 2022-11-01
      • 2018-11-24
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      相关资源
      最近更新 更多