react-native file upload to server working on iOS but not on Android

Viewed 1099

I am trying to send JSON files to a server using NodeJS with multer and I am able to send files in iOS but it is showing an error in Android.

var uploadUrl ='http://192.168.1.15:3333/SurveyJsonFiles/GetFiles/'
/*working for iOS*/

const data = new FormData();
data.append('files', {
    uri: filePath,
    type: 'multipart/form-data',
    name: fileName,
    });

const config = {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'multipart/form-data',
         },
    body: data,
    };

fetch(uploadUrl, config)
.then((checkStatusAndGetJSONResponse) => {
    console.log(checkStatusAndGetJSONResponse);
    }).catch((err) => {
       console.log(err)
       });

The error in android:

TypeError: Network request failed at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504) at XMLHttpRequest.dispatchEvent (event-target.js:172) at XMLHttpRequest.setReadyState (XMLHttpRequest.js:580) at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394) at XMLHttpRequest.js:507 at RCTDeviceEventEmitter.emit (EventEmitter.js:181) at MessageQueue.__callFunction (MessageQueue.js:366) at MessageQueue.js:106 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)

The code is working as expected in iOS.

What I have tried to fix

1) I have added cleartextTrafficPermitted="true" in Manifest.xml

2) I have tried using many react-native libraries like

a) react-native-background-upload .

b) react-native-uploader .

c) react-native-fs file upload .

3) I have tried by downgrading Android Version to 8.0

UPDATE

I changed the uri: filePath, to uri: 'file://'+filePath,

Then the error is resolved but the files are still not getting uploaded.

The new logs are as follows:

headers: Headers {map: {…}}
ok: true
status: 200
statusText: undefined
type: "default"
url: "http://192.168.1.15:3333/SurveyJsonFiles/GetFiles/"
_bodyBlob: Blob {_data: {…}}
_bodyInit: Blob {_data: {…}}
__proto__: Object

I am getting the same log for iOS as well but the files are getting uploaded.

0 Answers
Related