react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

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

Open cbpanchal opened this issue 5 years ago • 15 comments

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); });`

cbpanchal avatar May 14 '19 09:05 cbpanchal

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

hank121314 avatar May 21 '19 02:05 hank121314

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

cbpanchal avatar May 21 '19 05:05 cbpanchal

Stopdownload should work on android.

hank121314 avatar May 21 '19 05:05 hank121314

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

cbpanchal avatar May 21 '19 05:05 cbpanchal

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

cbpanchal avatar May 21 '19 06:05 cbpanchal

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

hank121314 avatar May 21 '19 06:05 hank121314

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

cbpanchal avatar May 21 '19 06:05 cbpanchal

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

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

hank121314 avatar May 21 '19 06:05 hank121314

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 ??

cbpanchal avatar May 21 '19 07:05 cbpanchal

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.

hank121314 avatar May 21 '19 07:05 hank121314

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

cbpanchal avatar May 23 '19 06:05 cbpanchal

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)

LordDraagonLive avatar Mar 21 '20 14:03 LordDraagonLive

same issue

Hoomanmsh avatar May 03 '20 16:05 Hoomanmsh

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

LordDraagonLive avatar May 03 '20 17:05 LordDraagonLive

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

ItaloSampaio avatar Feb 19 '21 20:02 ItaloSampaio