Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android issue uploading image to server using Axios in React-Native App

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"/>
like image 982
bob friston Avatar asked Jul 07 '17 08:07

bob friston


1 Answers

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;
}
like image 98
ajilantang Avatar answered Oct 06 '22 05:10

ajilantang