javascript - RNFS.uploadFiles 仅适用于原始上传,不适用于多部分/表单数据

标签 javascript reactjs react-native file-upload multer

我正在尝试将JSON文件上传到nodejs服务器,该服务器使用ng multer作为接收文件的中间件,并且RNFS.uploadFiles仅适用于原始Multer,不适用于具有特定字段的multer,例如(upload.txt)。单个('文件')

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代码

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

})

并且出现错误

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

最佳答案

我变了

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

进入

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

它解决了这个问题。

关于javascript - RNFS.uploadFiles 仅适用于原始上传,不适用于多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476996/

相关文章:

javascript - 在javascript中获取相对于另一个url的url

javascript - 获取深度嵌套的属性值

javascript - 停止 React JS/Flux 应用程序加载,直到一个 API 请求完成?

android - 无法使用android模拟器打开React Native项目

javascript - 我想使用 Javascript 操作 CSS

javascript - 在 UTC 中调用 toLocaleString()

reactjs - React-native-navigation 导航时更改屏幕方向

javascript - 在 react 渲染中使用异步和等待

javascript - react-native - 如何使用 FlatList 创建多个开关?

ios - React Native rnpm unlink 库