将空文件发送到服务器 React-Native



我正在尝试使用带有 multer 的 NodeJS 将 JSON 文件发送到服务器,我可以发送文件,但文件是空的。

我正在使用反应本机文件系统来循环访问文件夹中存在的所有文件

我没有收到任何错误,文件上传日志也显示为上传完成,但文件为空

我尝试使用表单数据发送它,但仍然没有运气

        var RNFS = require('react-native-fs');
        var path = RNFS.DocumentDirectoryPath + '/toBeSynced';
        RNFS.readDir(path)
            .then((success) => {
                success.forEach(function (element) {
                    var fileName = element.name
                    var filePath = element.path
                    const options = {
                        url: 'http://192.168.1.15:3333/SurveyJsonFiles/GetFiles',
                        path: filePath,
                        name: fileName,
                        field: 'files',
                        method: 'POST',
                        type: 'multipart',
                        headers: {
                            'content-type': 'multipart/form-data',
                        },
                        //Below are options only supported on Android
                        notification: {
                            enabled: true
                        }
                    }
                    Upload.startUpload(options).then((uploadId) => {
                        console.log('Upload started')
                        Upload.addListener('progress', uploadId, (data) => {
                            console.log(`Progress: ${data.progress}%`)
                        })
                        Upload.addListener('error', uploadId, (data) => {
                            console.log(`Error: ${data.error}%`)
                        })
                        Upload.addListener('cancelled', uploadId, (data) => {
                            console.log(`cancelled: ${data.error}%`)
                        })
                        Upload.addListener('completed', uploadId, (data) => {
                            // data includes responseCode: number and responseBody: Object
                            console.log('Completed!')
                        })
                    }).catch((err) => {
                        console.log('Upload error!', err)
                    })

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

您的路径不是绝对文件项路径。如果您有很多文件,请添加此代码

var files = [
  {
    name: 'test1',
    filename: 'test1.w4a',
    filepath: RNFS.DocumentDirectoryPath + '/test1.w4a',
    filetype: 'audio/x-m4a'
  }, {
    name: 'test2',
    filename: 'test2.w4a',
    filepath: RNFS.DocumentDirectoryPath + '/test2.w4a',
    filetype: 'audio/x-m4a'
  }
];

如果您只有一个文件,请像这样添加。

 var path = RNFS.DocumentDirectoryPath + '/toBeSynced/test.txt';

 var path = RNFS.DocumentDirectoryPath + '/test.txt';

你可以看看这些。

https://github.com/itinance/react-native-fs#examples

https://github.com/itinance/react-native-fs#constants

错误出在服务器端,因为nodemon在获取新文件时重新启动服务器

const data = new FormData();
data.append('files', {
    uri: filePath,
    type: 'multipart/form-data',
    name: fileName,
    });
const config = {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'multipart/form-data',
         },
    body: data,
    };
fetch(uploadUrl, config)
.then((checkStatusAndGetJSONResponse) => {
    console.log(checkStatusAndGetJSONResponse);
    }).catch((err) => {
       console.log(err)
       });

最新更新