6

I am getting the following network error when attempting to upload/POST an image from Android. Works perfectly on iOS. Works perfectly on Android when no image included in FormData.

export function formatAvatarData(uri) {
  const form = new FormData();
  form.append('avatar', {
    uri: uri,
    type: 'file',
    name: 'image.jpg',
  });
  return form;
}

iOS - [THIS WORKS AS EXPECTED] uri: file:///Users/bobfriston/Library/Developer/CoreSimulator/Devices/16E84C5E-2545-408C-B870-38054DFA3471/data/Containers/Data/Application/7D2F59E5-450A-4A60-962A-D5C368DCD8DF/Documents/images/F5AD02D9-6C3E-46E5-B54E-D5C98206BAAA.jpg

ANDROID URI - [THIS GIVES NETWORK ERROR AS BELOW] uri: content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F79/ORIGINAL/NONE/1408072274

Error: Network Error
    at createError (createError.js:15)
    at XMLHttpRequest.handleError (xhr.js:87)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378)

NOTES: "react-native": "=0.42.3"

AndroidManifest.xml has these permissions included:

android.permission.INTERNET"
android.permission.SYSTEM_ALERT_WINDOW"/>
android.permission.CAMERA" />
android.permission.WRITE_EXTERNAL_STORAGE"/>

3 Answers 3

8

Try to change type to image/jpeg:

export function formatAvatarData(uri) {
  const form = new FormData();
  form.append('avatar', {
    uri: uri,
    type: 'image/jpeg', // <-- this
    name: 'image.jpg',
  });
  return form;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I can confirm that the type that raise Network Error on Android
Have you got any solutions? I'm facing issue since 4 days and no updates.
1

The solution to the problem is here: https://github.com/facebook/react-native/issues/7306#issue-151988731

Comments

0

The below steps helped me resolve the same issue. Just in case helps,

  1. In android/app/src/main/java/com/{yourProject}/MainApplication.java comment the below line :

    initializeFlipper(this, getReactNativeHost().getReactInstanceManager())
    
  2. In android/app/src/debug/java/com/{yourProject}/ReactNativeFlipper.java comment line 43 :

    builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
    
  3. Code for image upload :

    var formData = new FormData();
       formData.append('UserId', '[email protected]');
       formData.append('VisitId', '28596');
       formData.append('EvidenceCapturedDate', '09/10/2019 13:28:20');
       formData.append('EvidenceCategory', 'Before');
       formData.append('EvidenceImage', {
         uri: Platform.OS === 'android' ? `file:///${path}` : `/private${path}`,
         type: 'image/jpeg',
         name: 'image.jpg',
       });
       axios({
         url: UrlString.BaseUrl + UrlString.imageUpload,
         method: 'POST',
         data: formData,
         headers: {
           Accept: 'application/json',
           'Content-Type': 'multipart/form-data'
         },
       })
         .then(function (response) {
           console.log('*****handle success******');
           console.log(response.data);
    
         })
         .catch(function (response) {
           console.log('*****handle failure******');
           console.log(response);
         });
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.