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

How to upload a file ? Request for proper syntax. Socket Closed

Open cullsin opened this issue 6 years ago • 5 comments

I am using react-native-image-picker to get the image properties and use the below code to push the files to my server. I am getting a socket closed error. Can you please suggest me the valid syntax to upload a image file in android as well as in ios.

    export function postImage(state) {
      return RNFS.uploadFiles({
        toUrl: $http.defaults.baseURL + '/image/save',
        files: [
          {
            filepath: RNFS.ExternalStorageDirectoryPath + state.photo.fileName,
            filetype: state.photo.type,
            name: state.photo.fileName,
            filename: state.photo.fileName
          }
        ],
        method: 'POST',
        headers: {
          'content-type': 'multipart/form-data'
        },
        fields: {
          'hello': 'world',
        },
      }).promise
        .then(response => response)
        .catch(error => alert(JSON.stringify(error)));
    }

cullsin avatar Apr 04 '20 08:04 cullsin

Hi,

I tried and nothing works. I guess the problem is not with data or the format or the option. Something blocks the socket. Am I missing something?

Raja K

On Sun, 5 Apr 2020 at 05:08, Saadi Soulaimane [email protected] wrote:

check this #724 https://github.com/itinance/react-native-fs/issues/724

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/itinance/react-native-fs/issues/873#issuecomment-609104620, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHGJPTTL5YE4VHMSGIPG43RK7AG5ANCNFSM4L5UQGCA .

cullsin avatar Apr 05 '20 05:04 cullsin

Hi,

The issue is with form data. I successfully communicate to the backend, but my no file information available in the request.

    export const postImage = async state => {

      const formData = new FormData();
      formData.append('image[uri]', 'file://' + state.photo.path)
      formData.append('image[type]', state.photo.type)
      formData.append('image[name]', state.photo.fileName)

      return fetch($http.defaults.baseURL + '/image/save', {
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      body: formData
      }).then(response => response)
      .catch(error => error);
    }  

cullsin avatar Apr 05 '20 08:04 cullsin

Hi @cullsin ! What server did you use? express or django or something else?

And the name in the files array should not be the filename, it should be the form data name.

Take multer for example:

If server side setting is:

app.post('/profile', upload.single('avatar'), function (req, res, next) {})

then files should be like:

files: [
          {
            filepath: RNFS.ExternalStorageDirectoryPath + state.photo.fileName,
            filetype: state.photo.type,
            name: 'avatar',
            filename: state.photo.fileName
          }
        ],

hank121314 avatar May 04 '20 08:05 hank121314

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

In my case, nodemon was to blame. While uploading the file, it restarted the server causing the "Socket Closed" error. There is no problem with this wonderful library. At least not in that regard.

Start the server normally, with node or ts-node, and see if it worked.

leonardolima99 avatar Mar 14 '21 23:03 leonardolima99