gpt4 book ai didi

javascript - RNFS.uploadFiles 仅适用于原始上传,不适用于多部分/表单数据

转载 作者:行者123 更新时间:2023-12-02 23:08:35 42 4
gpt4 key购买 nike

我正在尝试将JSON文件上传到nodejs服务器,该服务器使用ng multer作为接收文件的中间件,并且RNFS.uploadFiles仅适用于原始Multer,不适用于具有特定字段的multer,例如(upload.txt)。单个('文件')

var file = [
{
name: newpath,
filename: newpath,
filepath: newpath,
filetype: 'json'
}
];


var uploadBegin = (response) => {
var jobId = response.jobId;
console.log('UPLOAD HAS BEGUN! JobId: ' + jobId);
};

var uploadProgress = (response) => {
var percentage = Math.floor((response.totalBytesSent / response.totalBytesExpectedToSend) * 100);
console.log('UPLOAD IS ' + percentage + '% DONE!');
};

RNFS.uploadFiles({
toUrl: uploadUrl,
files: file,
method: 'POST',
headers: {
'Accept': 'application/json',
},

begin: uploadBegin,
progress: uploadProgress
}).promise.then((response) => {
if (response.statusCode == 200) {
console.log('FILES UPLOADED!');
} else {
console.log('SERVER ERROR');
}
})
.catch((err) => {
if (err.description === "cancelled") {
// cancelled by user
}
console.log(err);
});

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

multer代码

var upload = multer({
storage: storage,// fileFilter: fileFilter
});

router.post('/GetFiles', upload.single('files'), function (req, res) {


if (req.file) {
res.send({ "Status": true, "Msg": "File Uploaded Successfully" });
}
else {
res.send({ "Status": false, "Msg": "Failed to upload the File", "body": req.body });
}

})

并且出现错误

Error: ENOENT: no such file or directory, open 'http://192.168.1.15:3333/SurveyJsonFiles/GetFiles/' at createErrorFromErrorData (NativeModules.js:155) at NativeModules.js:104 at MessageQueue.__invokeCallback (MessageQueue.js:414) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:80

最佳答案

我变了

 var file = [
{
name: newpath,
filename: newpath,
filepath: newpath,
filetype: 'json'
}
];

进入

 var file = [
{
name: 'files',
filename: newpath,
filepath: newpath,
filetype: 'json'
}
];

它解决了这个问题。

关于javascript - RNFS.uploadFiles 仅适用于原始上传,不适用于多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476996/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com