一次性上传多个大型 json 文件 React-native to NodeJS
Uploading multiple large json files in one go React-native to NodeJS
我正在尝试将多个大型 JSON 文件从 React-native 上传到节点 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 文件夹移到了项目范围之外
我正在尝试将多个大型 JSON 文件从 React-native 上传到节点 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 文件夹移到了项目范围之外