简体   繁体   English

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

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

I am trying to upload multiple large size JSON files from React-native to node js. 我正在尝试将多个大型JSON文件从React-native上传到Node js。

The files are being uploaded unless the file in larger in size, in which case, it does not upload in one try. 除非文件较大,否则文件将被上传,在这种情况下,将不会尝试一次上传。

I suspect that: 我怀疑:
Since the upload code is in a for loop the code is starting the upload but not waiting for the file to upload and starting to upload the next file 由于上载代码处于for循环中,因此该代码正在开始上载,但不等待文件上载并开始上载下一个文件

Is there any way to ensure that each file gets uploaded in one go? 有什么方法可以确保一次上传每个文件?

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

The JSON files will more than 50Mb depending on data, since it contains base64 image data the size will increase as the user takes more photos. 取决于数据,JSON文件将超过50Mb,因为它包含base64图像数据,其大小将随着用户拍摄更多照片而增加。

The app will be creating new files when the user records any information, There is no error message displayed for partial file upload. 当用户记录任何信息时,该应用将创建新文件。对于部分文件上传,不会显示错误消息。

The this.moveSyncedFiles() is moving the synced files to another folder so that the same file does not get uploaded multiple times 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