【问题标题】:Network request failed in react native while uploading images to server将图像上传到服务器时,网络请求在本机反应中失败
【发布时间】:2021-03-10 06:16:50
【问题描述】:

我正在尝试使用 fetch 将图像上传到服务器。但我收到类似 = 网络请求失败的错误。 我的代码有什么问题请帮忙。

这是我的代码


const App = () => {
 
  const [singleFile, setSingleFile] = useState(null);

  const uploadImage = async () => {

    if (singleFile != null) {
  
      const fileToUpload = singleFile;

      const data = new FormData();
      data.append("lead_tag_number", "NAS00001");
      data.append("upload_pan_card_file", fileToUpload)
  
      let res = await fetch(
        'https://nasdigital.tech/Android_API_CI/upload_multipart_data',
        {
          method: 'post',
          
          headers: {
            
            'Content-Type': 'multipart/form-data;',
            
          },
           body : data,
        }
      )
  .then(response => response.text())

  .then(result => console.log(result))
  .catch(error => console.log('error', error));
  };
}


  const selectFile = async () => {
      let pickerResult = await ImagePicker.launchImageLibraryAsync({
        allowsEditing: true,
        // base64: true,
        aspect: [4, 3],
      })

        setSingleFile({ singleFile : pickerResult.uri});
  };
    </View>
  );
};

请忽略这个。我正在尝试使用 fetch 将图像上传到服务器。但我收到类似 = 网络请求失败的错误。 我的代码有什么问题请帮忙。我正在尝试使用 fetch 将图像上传到服务器。但我收到类似 = 网络请求失败的错误。 我的代码有什么问题请帮忙。

【问题讨论】:

    标签: react-native multipartform-data multipart


    【解决方案1】:

    将此添加到您的 info.plist 中:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

    【讨论】:

    • 您是从模拟器还是从物理设备运行?如果是模拟器,请尝试从物理设备运行它以检查系统中可能存在的代理问题
    • info.plist 文件在哪里
    猜你喜欢
    • 2020-11-21
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2019-09-16
    • 2018-03-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多