首页 > 解决方案 > 图像未在 React Native 中上传到 PHP 服务器

问题描述

我正在尝试将图像从 react native 上传到 PHP 服务器。但我无法上传图片。我尝试所有的东西。我没有收到任何错误,警告。没有任何单一的错误。我仍然无法将图像上传到服务器。为什么?????

这是我的代码

onst App = () => {

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

  const uploadImage = async () => {

    if (singleFile != null) {

       const formData = new FormData();
      formData.append("lead_tag_number", "NAS00001");
      formData.append("upload_pan_card_file", singleFile.singleFile.replace('file:/','file://'))
    
       console.log(formData._parts[1][1])
      
      fetch('https://xyz.tech/Android_API_CI/up_data',
        {
          method: 'post',
            // body: JSON.stringify([{formData}]),
            body : formData._parts[1][1],
          headers: {
            
            'Content-Type': 'multipart/form-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});
        console.log(singleFile.singleFile.replace('file:/','file://'))
   
  };

我收到这样的服务器响应

here------------1<pre>files dataArray
(
// I want My Image Here //
)
here you are

请帮忙

标签: react-nativemultipartform-datamultipart

解决方案


推荐阅读