2

I use react-native-image-picker to upload image to a server. I use the following code:

 sendPhoto = async () =>{
  const fileToUpload = {
    type:'image/jpg', 
    uri: 'file://'+this.state.photo.path,
     name:'uploadimage.jpg'
    }
  console.log(fileToUpload);
  let data = new FormData();
 data.append('file_attachment', { type:'image/jpg', uri: this.state.photo.path, name:'uploadimage.jpg'})
   fetch (settings.ajaxurl+'sickFishUpload',{
        method: 'POST',
        body: data,
        headers: {
            'Content-Type': 'multipart/form-data; ',
          },  
      })
       .then( (response) => response.json())
       .then((res) => {
        console.log(res);          
        //console.log(res);
       })
       .catch(function(error) {
      console.log('There has been a problem with your fetch operation: ' + error.message);
   // ADD THIS THROW error
    throw error;
  })

}

Unfortunatelly It cannot communicate with the server. I got this message: There has been a problem with your fetch operation: TypeError: Network request failed.

If I erase this json from the data:

{ type:'image/jpg', uri: this.state.photo.path, name:'uploadimage.jpg'} 

it can communicate with the server. I set the AndroidManifest.xml with

 <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and the application tag with

android:requestLegacyExternalStorage="true"

I'm totally stucked. Do you have any idea what is wrong? Of course I 've found this question in a previous post, but It is more than a years old and my solution is worked with the previous version of react-native. So I don't know how to upgrade my code... Every suggestion is very welcome.

thx Adam

0 Answers0