我目前正在为VR游戏Beat Saber编写一个mod,用于重播播放器的性能并将其上传到Express Node.JS服务器进行存储。但是,无论何时上传重播(另存为JSON),我的服务器都会将req.body
作为空对象返回,并且req.file
是未定义的。
我正在使用bodyparser
解析JSON和multer
解析Express代码中的文件。
我尝试过:
form.AddField()
)使用Postman来测试我的服务器代码,服务器可以很好地处理Postman的请求,所以我得出的结论是这是客户端问题。
C#上传代码:
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection(string.Format("songName={0}&songSubName={1}&authorName={2}&difficulty={3}", songName, songSubName, authorName, difficulty)));
formData.Add(new MultipartFormFileSection("replayData", Encoding.UTF8.GetBytes(builder.ToString() /*Compressed JSON*/), "file", "application/json"));
UnityWebRequest w = UnityWebRequest.Post("http://caed.jackz.me/ghostmode/UploadReplay", formData);
w.SetRequestHeader("Content-Type", "application/json; charset=utf8");
w.chunkedTransfer = false; //Some of this code was trial and error through others question
UnityWebRequestAsyncOperation request = w.SendWebRequest();
request.completed += uploadComplete;
快递代码:
app.post("/ghostmode/UploadReplay", upload.array(), (req,res) => {
console.log(moment.tz(moment(), "America/Los_Angeles").format("h:mm:ss A"));
console.log(req.body); //bodyparser.json()
console.log(req.files); //multer
});
服务器的控制台输出始终为
7:54:07 PM //Timestamp so I know when its from
{} //req.body via bodyparser
undefined //req.files via multer