【问题标题】:How to post image and array together with react native to backend?如何将图像和数组与本机反应一起发布到后端?
【发布时间】:2020-11-13 11:49:52
【问题描述】:

我正在尝试使用本机反应将图像文件和数据(对象或数组)发送到后端。我尝试使用 fetch 和下面的代码并让它将图像发送到后端。但我的目标是将图像和数据一起发送。我怎样才能做到这一点?


uploadPhoto = async (response) => {


 

    const formData = new FormData();
    formData.append('fileToUpload', {
      uri: response.path,
      name: 'image',
      type: response.mime,
      imgPath: response.path
    });

    const infos={ad:'onur',soyad:'cicek'};



    try {

      const rest = await fetch('https://www.birdpx.com/mobile/fotografyukle/'+this.state.user.Id, {
        method: 'POST',
        headers: { 'Accept': 'application/json', 'Content-type': 'multipart/form-data' },
        body: formData
        });

        const gelenVeri = await rest.text();
        let convertedVeri=JSON.parse(gelenVeri);

        console.log(convertedVeri);
        return false


      
    } catch (err) {
      console.log(err)
    }

  };

我需要发布const infos={ad:'onur',soyad:'cicek'}; 和图片。

【问题讨论】:

    标签: javascript reactjs react-native post fetch


    【解决方案1】:

    您可以在 formData 本身中传递它

    formData.append('other_infos', infos);
    

    这里other_infos是需要从后端提取的key,你可以在FormData中附加n个参数

    【讨论】:

      【解决方案2】:

      您可以通过将其他数据添加到 FormData 来实现。 就像例子中的一样

      formData.append('infos', infos);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-25
        • 2012-05-28
        • 2019-03-19
        • 1970-01-01
        • 2018-02-16
        • 2021-09-24
        • 2018-05-28
        • 1970-01-01
        相关资源
        最近更新 更多