0

I am trying to upload images to server using fetch. But I am getting error like = Network request failed. what is wrong with my code please help.

here is my code


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

please ignore this . I am trying to upload images to server using fetch. But I am getting error like = Network request failed. what is wrong with my code please help.I am trying to upload images to server using fetch. But I am getting error like = Network request failed. what is wrong with my code please help.

1 Answers1

0

Add this in your info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Surya
  • 546
  • 7
  • 13
  • are you running from emulator or from a physical device? if it is emulator please try to run it from a physical device to check for the possible proxy issue from your system – user10384449 Mar 10 '21 at 06:50
  • where is info.plist file eist –  Mar 10 '21 at 06:51
  • @SohilShaikh https://stackoverflow.com/questions/6853813/where-can-i-find-info-plist-file – Surya Mar 10 '21 at 06:55