Ad

RNFS.uploadFiles Only Works For Raw Uploads Not For Multipart/form-data

I am trying to upload JSON files to the nodejs server which is usng multer as a middleware to receive files and the RNFS.uploadFiles only works for raw Multer and does not work for the multer which has a specific field eg(upload.single('files')

var file = [
                {
                    name: newpath,
                    filename: newpath,
                    filepath: newpath,
                    filetype: 'json'
                }
            ];


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

            var uploadProgress = (response) => {
                var percentage = Math.floor((response.totalBytesSent / response.totalBytesExpectedToSend) * 100);
                console.log('UPLOAD IS ' + percentage + '% DONE!');
            };

            RNFS.uploadFiles({
                toUrl: uploadUrl,
                files: file,
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                },

                begin: uploadBegin,
                progress: uploadProgress
            }).promise.then((response) => {
                if (response.statusCode == 200) {
                    console.log('FILES UPLOADED!'); 
                } else {
                    console.log('SERVER ERROR');
                }
            })
                .catch((err) => {
                    if (err.description === "cancelled") {
                        // cancelled by user
                    }
                    console.log(err);
                });

            // }
        })
        .catch((err) => {
            console.log(err.message);
        });

multer code

var upload = multer({
storage: storage,// fileFilter: fileFilter
});

router.post('/GetFiles', upload.single('files'), function (req, res) {


if (req.file) {
    res.send({ "Status": true, "Msg": "File Uploaded Successfully" });      
}
else {      
    res.send({ "Status": false, "Msg": "Failed to upload the File", "body": req.body });
}

})

and is giving error

Error: ENOENT: no such file or directory, open 'http://192.168.1.15:3333/SurveyJsonFiles/GetFiles/' at createErrorFromErrorData (NativeModules.js:155) at NativeModules.js:104 at MessageQueue.__invokeCallback (MessageQueue.js:414) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:80

Ad

Answer

I changed

 var file = [
            {
                name: newpath,
                filename: newpath,
                filepath: newpath,
                filetype: 'json'
            }
        ];

into

 var file = [
            {
                name: 'files',
                filename: newpath,
                filepath: newpath,
                filetype: 'json'
            }
        ];

it fixed this issue.

Ad
source: stackoverflow.com
Ad