我使用Unity的UnityWebRequest API(Unity 2019.2.13f1)具有以下上传代码:
public IEnumerator UploadJobFile(string jobId, string path)
{
if (!File.Exists(path))
{
Debug.LogError("The given file to upload does not exist. Please re-create the recording and try again.");
yield break;
}
UnityWebRequest upload = new UnityWebRequest(hostURL + "/jobs/upload/" + jobId);
upload.uploadHandler = new UploadHandlerFile(path);
upload.downloadHandler = new DownloadHandlerBuffer();
upload.method = UnityWebRequest.kHttpVerbPOST;
upload.SetRequestHeader("filename", Path.GetFileName(path));
UnityWebRequestAsyncOperation op = upload.SendWebRequest();
while (!upload.isDone)
{
//Debug.Log("Uploading file...");
Debug.Log("Uploading file. Progress " + (int)(upload.uploadProgress * 100f) + "%"); // <-----------------
yield return null;
}
if (upload.isNetworkError || upload.isHttpError)
{
Debug.LogError("Upload error:\n" + upload.error);
}
else
{
Debug.Log("Upload success");
}
// this is needed to clear resources on the file
upload.Dispose();
}
string hostURL = "http://localhost:8080";
string jobId = "manualUploadTest";
string path = "E:/Videos/short.mp4";
void Update()
{
if (Input.GetKeyDown(KeyCode.O))
{
Debug.Log("O key was pressed.");
StartCoroutine(UploadAndTest(jobId, path));
}
}
我在服务器端收到的文件损坏了,尤其是如果它们很大(30 MB或更大)。它们最后缺少字节,有时中间会重复整个字节块。
我注意到,如果我注释掉对upload.uploadProgress
的访问权限(例如,而是使用上面的注释掉的调试行仅打印字符串文字),则文件将保持完整。完全放弃wile循环并将其替换为yield return op
也可以。
我在外循环中反复测试了这种奇怪的行为-通常在使用“错误”代码重复最多8次之后,文件似乎已损坏。如果我使用“正确”变体,则连续成功上传了100次(更新:500次)。
是否有upload.uploadProgress
的副作用?就其价值而言,如果我打印op.progress
也会发生同样的情况-文件也损坏了。
答案 0 :(得分:0)
这听起来像一个真正的错误。 uploadProgress
显然不应有副作用。