Hello,
I’m developing a very simple piece of code for uploading a file to my Server. On my Server, I have a PHP script with just 3 lines of code. I’m using Unity 2019.4.28f1.
The upload process works and I’m able to upload my files to my Server without any issue.
However, I want to track the upload process, simply using the uploadProgress and the uploadedBytes properties for getting this information.
Unlike the download operation, where downloadProgress and downloadedBytes work perfectly, in the upload process these two properties don’t work as expected: uploadProgress is always 1 (aka 100%) and uploadedBytes has always the full size of the file.
This is the code:
WWWForm form = new WWWForm();
...
form.AddBinaryData("file", data, fileName);
www = UnityWebRequest.Post("http://www.myserver.com/myuploader.php", form);
www.SendWebRequest();
// This code is in a cycle:
Debug.Log(www.uploadProgress)
Debug.Log(www.uploadedBytes)
Is it a bug? Is it a misconfiguration of my server? Is there a solution?
How big is the file you’re uploading? Progress will not be reported at a high granularity due to buffering and packet sizes, so the progress might jump directly to 1.
I did some tests with 500Kb, 1Mb, 5Mb, 10Mb, 20Mb, 50Mb and 100Mb.
For example, with the 100Mb file it tooks 30 seconds for finishing the uploading. During this period, I call the uploadProgress and the uploadedBytes every 250 ms, so 4 times per second.
the upload process works. After a while, my file is correctly uploaded and the www.isDone is accurately fired, informing me everything is gone perfectly;
instead, the downloadHandler of UnityWebRequest works perfectly and the statuses of the download process are returned correctly.
Note: I tested it also in Unity 2022.1.20f1, and the issue persists.
private void StartUpload(string myFile)
{
// I read all the bytes from my file
var data = System.IO.File.ReadAllBytes(myFile);
// I prepare a WWWForm with the read bytes and a file name
WWWForm form = new WWWForm();
form.AddBinaryData("file", data, "myfile.dat");
// I start the upload process
www = UnityWebRequest.Post("http://mysite.com/myuploader.php", form);
www.SendWebRequest();
// I start a simple async function for getting the upload status
UploadStatus();
}
private async void UploadStatus()
{
// I just wait for a little
await Task.Delay(250);
if (IsError())
{
Debug.LogError("OPS!!!");
}
else if (www.isDone)
{
Debug.Log("Yeah! My file is uploaded!");
}
else
{
// It should give me a value between 0 and 1, but it immediately returns 1
Debug.Log("Progress: " + www.uploadProgress);
// It should give me the bytes actually uploaded, but it immediately returns the full file size
Debug.Log("Uploaded bytes: " + www.uploadedBytes);
// I repeat this function for getting updated information.
UploadStatus();
}
}
Have you tried it without the quarter second delay?
The initial zero is expected delay while connection is established and TLS handshake. After that it may actually be the case that all data was sent. The progress is not the data send, but rather it being written to socket.
Hi,
I removed the await Task.Delay(250) and, as a result, I simply obtained that the UploadStatus() function is executed faster, without that delay. The issue remained.