繁体   English   中英

一站式上传多个大型json文件到NodeJS

[英]Uploading multiple large json files in one go React-native to NodeJS

我正在尝试将多个大型JSON文件从React-native上传到Node js。

除非文件较大,否则文件将被上传,在这种情况下,将不会尝试一次上传。

我怀疑:
由于上载代码处于for循环中,因此该代码正在开始上载,但不等待文件上载并开始上载下一个文件

有什么方法可以确保一次上传每个文件?

syncFunction() {
  var RNFS = require('react-native-fs');
  var path = RNFS.DocumentDirectoryPath + '/toBeSynced';
  RNFS.readDir(path)
  .then((success) => {
    for (let i = 0; i < success.length; i++) {
      var fileName = success[i].name
      var filePath = success[i].path
      var uploadUrl = 'http://192.168.1.15:3333/SurveyJsonFiles/GetFiles/'

      if (Platform.OS === 'android') {
        filePath = filePath.replace("file://", "")
      } else if (Platform.OS === 'ios') {
        filePath = filePath
      }
      const data = new FormData();
      data.append("files", {
        uri: filePath,
        type: 'multipart/form-data',
        name: fileName,
      });

      const config = {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
        },
        body: data,
      };

      fetch(uploadUrl, config)
      .then((checkStatusAndGetJSONResponse) => {
        console.log(checkStatusAndGetJSONResponse);
        this.moveFile(filePath, fileName)
      }).catch((err) => {
        console.log(err)
      });
    }
  })
  .catch((err) => {
    console.log(err.message);
  });
}

取决于数据,JSON文件将超过50Mb,因为它包含base64图像数据,其大小将随着用户拍摄更多照片而增加。

当用户记录任何信息时,该应用将创建新文件。对于部分文件上传,不会显示错误消息。

this.moveSyncedFiles()将同步的文件移动到另一个文件夹,以使同一文件不会多次上传

 moveFile(oldpath, oldName) {
  var syncedPath = RNFS.DocumentDirectoryPath + '/syncedFiles'
  RNFS.mkdir(syncedPath)
  syncedPath = syncedPath + "/" + oldName
  RNFS.moveFile(oldpath, syncedPath)
    .then((success) => {
      console.log("files moved successfully")
      })
    .catch((err) => {
      console.log(err.message)
      });
    }

事实证明,故障出在nodejs端,并且每次发现新文件时nodemon都会重新启动服务器,因此我们只是将uploads文件夹移到了项目范围之外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM