0

I'm creating an application in Unity3d that needs to download images from a database via a PHP file, for various reasons I decided to use System.Net.WebClient instead of the unity class WWW and after several attempts it works with this code.

    idEvent = 3; //exemple
    string responsefromserver = "";
    Uri URL = new Uri("http://******.000webhostapp.com/ImEvents.php");
    WebClient webClient = new WebClient();

    NameValueCollection formData = new NameValueCollection();
    formData["selectedId"] = idEvent;

    webClient.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
    {
        Debug.Log ( string.Format("  downloaded {0} of {1} bytes.",
        e.BytesReceived,
        bytes //e.TotalBytesToDownload returns always -1 so this value is given by a variable
        ));
    };
    webClient.UploadValuesCompleted += (object sender, UploadValuesCompletedEventArgs e) =>
    {
        responsefromserver = Encoding.UTF8.GetString(e.Result);
        webClient.Dispose();
    };
    webClient.UploadValuesAsync(URL, "POST", formData);

the e.TotalBytesToDownload returns always -1 but I noticed after various research that this is due to other factors and since it is an information that I can recover in another way this does not matter to me ... The problem is that e.BytesRecived immediately rises to figures around 3000-4000 bytes and then RETURN BACK and give me all values ​​that oscillate between 1444 and 1452 bytes ...

THIS DOES NOT HAVE ANY SENSE!!!

here is an example of the Log

  downloaded 6828 of 39680 bytes.
  downloaded 2644 of 39680 bytes.
  downloaded 1452 of 39680 bytes.
  downloaded 1447 of 39680 bytes.
  downloaded 1444 of 39680 bytes.
  downloaded 1452 of 39680 bytes.
  ecc...

So my question is:

Why e.BytesReceived does not give me the real number of bytes recived (which should only increase up to the file size and not oscillate between 1444 and 1452 bytes)

(If it is important, this code is inside of an IEnumerator function...)

I apologize for my english

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
  • What's your question? – CodeCaster Aug 21 '18 at 14:08
  • @CodeCaster My question is: Why e.BytesReceived does not give me the real number of bytes recived (which should only increase up to the file size and not oscillate between 1444 and 1452 bytes) – Emanuele Scarsella Aug 21 '18 at 14:51
  • It's the number of bytes received since the last time the event fired. – CodeCaster Aug 21 '18 at 14:59
  • *"I decided to use System.Net.WebClient instead of the unity class WWW"* Why? There are few instances where WebClient i recommended.... Why not use `WWW`? – Programmer Aug 21 '18 at 15:20
  • @Programmer I noticed that with WebClient I can download images with about 0.1 seconds less every 100KB – Emanuele Scarsella Aug 21 '18 at 15:26
  • @CodeCaster oohhh thanks... in the MSDN they just say that it returns an Int64 value which indicates the number of bytes received. [link](https://msdn.microsoft.com/it-it/library/system.net.downloadprogresschangedeventargs.bytesreceived(v=vs.110).aspx) thank you very much and sorry for the question and my stupidity : - ( – Emanuele Scarsella Aug 21 '18 at 15:37
  • @Programmer ... and also because WWW.Progress is not accurate... – Emanuele Scarsella Aug 21 '18 at 15:45
  • @EmanueleScarsella I was about to reply but you deleted your question. How are you using it? I think that I might be able to help you if you actually put the complete WWW code that didn't work for you. Also where do you put the download images? What do you use them for? Unity has better support with WWW then WebClient – Programmer Aug 21 '18 at 15:47
  • @Programmer thanks for your availability ... [here](https://gist.github.com/emanuele-scarsella/f2e9649919fce39c76ddfc5949fd428b) is the previous code with the WWW class ... remamber that the main problem with WWW is not the time but is the inprecision with WWW.progres – Emanuele Scarsella Aug 21 '18 at 17:16
  • The `yield return new WaitForSeconds(0.01f)` should just be `yield return null`. Note that you should be using `UnityWebRequest` instead of `WWW`. It replaced `WWW`. See [this](https://stackoverflow.com/questions/46003824/sending-http-requests-in-c-sharp-with-unity/46008025#46008025) for many examples. For just downloading images, use `UnityWebRequestTexture.GetTexture` which is optimized for that. See [this](https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html) for example. – Programmer Aug 21 '18 at 17:27
  • @Programmer thank you very much, I will try it as soon as possible and I will evaluate the result... certainly will solve my problems with the loading bar and I hope also better loading times. I will say to you :-) – Emanuele Scarsella Aug 21 '18 at 17:47

1 Answers1

1

As @CodeCaster pointed out in the comments DownloadProgressChangedEventArgs.BytesReceived It's the number of bytes received since the last time the event fired and not as I had mistakenly thought the total number of bytes downloaded so far.

I apologize for asking a possibly unnecessary question but unfortunately in the MSDN they have not been very clear in simply saying that DownloadProgressChangedEventArgs.BytesReceived is An Int64 value that indicates the number of bytes received.