React-native-fs: Upload files in android not working, sometimes i got Error: Socket closed and request is not sent

Created on 14 May 2019  路  15Comments  路  Source: itinance/react-native-fs

it's not sending any request to my server and sometimes it throws a socket closed error, please check my current code that I have used.

Error:
Error: Socket closed at createErrorFromErrorData (NativeModules.js:146) at NativeModules.js:95 at MessageQueue.__invokeCallback (MessageQueue.js:397) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:297) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:72

Here is my code

`const toUrl = 'http://*';
const name = 'file_XCzY_SINGLE_FILE_POST';
const filename = 'IMG-20190514-WA0003.jpg';
const filepath = 'content://com.android.providers.media.documents/document/image%3A209152';
const filetype = 'image/jpeg';

const params = {
c2f: "HP4S",
isIOS: false,
meta_Chartfield_Number: '121212',
meta_Comments: "",
meta_Department: "%user_ldap_department%",
meta_Due_date_Poster_Order_Form: null,
meta_Due_time_Poster_Order_Form: "4 pm",
meta_Email: "%user_ldap_mail%",
meta_Name123: "%user_ldap_displayName%",
name: "file_XCzY_SINGLE_FILE_POST",
the_action: "STOR",
uploadPath: "%2FTest%2F"
};

const uploadBegin = (response) => {
const jobId = response.jobId;
console.log('UPLOAD HAS BEGUN! JobId: ' + jobId);
};

const uploadProgress = (response) => {
const percentage =
Math.floor((response.totalBytesSent/response.totalBytesExpectedToSend)

  • 100);
    console.log('UPLOAD IS ' + percentage + '% DONE!');
    };

RNFS.uploadFiles({
toUrl: encodeURI(url),
files: [{
name
filename
filepath
filetype
}],
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Cookie': 'my-cookies*'
},
fields: params,
begin: uploadBegin,
beginCallback: uploadBegin,
progressCallback: uploadProgress,
progress: uploadProgress
}).promise.then((response) => {
console.log(response,"<<< Response");
if (response.statusCode == 200) {
console.log('FILES UPLOADED!');
} else {
console.log('SERVER ERROR');
}
})
.catch((err) => {
if (err.description) {
switch (err.description) {
case "cancelled":
console.log("Upload cancelled");
break;
case "empty":
console.log("Empty file");
default:
//Unknown
}
} else {
//Weird
console.log('Weird')
}
console.log(err);
});`

Most helpful comment

Hey did you guys found a solution to this? I'm getting the same error
Error: Socket closed at Object.fn [as uploadFiles] (NativeModules.js:99) at Object.uploadFiles (FS.common.js:594) at App.js:110 at JSTimers.js:250 at _callTimer (JSTimers.js:146) at Object.callTimers (JSTimers.js:399) at MessageQueue.__callFunction (MessageQueue.js:436) at MessageQueue.js:111 at MessageQueue.__guard (MessageQueue.js:384) at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:110)

All 15 comments

const filepath = 'content://com.android.providers.media.documents/document/image%3A209152';
filepath should begin with file,like "file://".
UploadFiles doesn't support the content in android currently.
You can use RNFS to get the filepath

const filepath = 'content://com.android.providers.media.documents/document/image%3A209152';
filepath should begin with file,like "file://".
UploadFiles doesn't support the content in android currently.
You can use RNFS to get the filepath

let me check if it works, i have another quetions is stopdownload works on android or not?? Thanks

Stopdownload should work on android.

Stop download should work on Android.

but as per documentation, it is working on iOS only, but I need stop download in Android as well

const filepath = 'content://com.android.providers.media.documents/document/image%3A209152';
filepath should begin with file,like "file://".
UploadFiles doesn't support the content in android currently.
You can use RNFS to get the filepath

let me check if it works, i have another quetions is stopdownload works on android or not?? Thanks

still it is not working i have replaced content:// to file:// . I faced same issue not requestting on server

Not only replaced content:// to file:// . You should use RNFS to get the full file path like "file:///mnt/sdcard/FileName.jpeg".

Not only replaced content:// to file:// . You should use RNFS to get the full file path like "file:///mnt/sdcard/FileName.jpeg".

But how ?? can you please explain with some pseudo code ?? so I can try with that it will really helpful for me Thanks

It depends on the location of your file. If your file is stored in sdcard use RNFS. ExternalStorageDirectoryPath

const filepath = RNFS. ExternalStorageDirectoryPath + "FileName.jpeg"

It depends on the location of your file. If your file is stored in sdcard use RNFS. ExternalStorageDirectoryPath

const filepath = RNFS. ExternalStorageDirectoryPath + "FileName.jpeg"

It is possible to get an original path using https://github.com/itinance/react-native-fs#statfilepath-string-promisestatresult ??

It depends on the location of your file. If your file is stored in sdcard use RNFS. ExternalStorageDirectoryPath

const filepath = RNFS. ExternalStorageDirectoryPath + "FileName.jpeg"

It is possible to get an original path using https://github.com/itinance/react-native-fs#statfilepath-string-promisestatresult ??

I haven't tried it before. But according to the source code, it should work.

Hello @hank121314 Do you have any idea about this one [https://github.com/itinance/react-native-fs/issues/679]

Hey did you guys found a solution to this? I'm getting the same error
Error: Socket closed at Object.fn [as uploadFiles] (NativeModules.js:99) at Object.uploadFiles (FS.common.js:594) at App.js:110 at JSTimers.js:250 at _callTimer (JSTimers.js:146) at Object.callTimers (JSTimers.js:399) at MessageQueue.__callFunction (MessageQueue.js:436) at MessageQueue.js:111 at MessageQueue.__guard (MessageQueue.js:384) at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:110)

same issue

For me the issue was with the file URI that I get when I pick a file using from react-native-fs. Therefore, I used npm install react-native-file-picker@latest --save package for file picking and then uploaded using react-native-fs

@Hoomanmsh, @LordDraagonLive, see if this can help you: https://github.com/itinance/react-native-fs/issues/973#issuecomment-782301107

Was this page helpful?
0 / 5 - 0 ratings

Related issues

barnir picture barnir  路  3Comments

wangfpp picture wangfpp  路  3Comments

JohnRyanTsai picture JohnRyanTsai  路  3Comments

let-aurn picture let-aurn  路  3Comments

quietautumn picture quietautumn  路  4Comments