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

时间:2019-06-21 14:20:42

标签: javascript reactjs react-native file-upload multer

我正在尝试使用带有multer的NodeJS将JSON文件发送到服务器,并且能够发送文件,但是文件为空。

我正在使用React-native-File-System遍历文件夹中存在的所有文件

我没有收到任何错误,文件上传日志也显示为“ UPLOAD COMPLETED”,但文件为空

我尝试发送带有表单数据的邮件,但还是没有运气

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

  

在iOS上无法在Android上运行

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