2

I just wanted to know if there is any way I can have the download speed of a file, like in utorrent, for example using php or javascript ? :) I have tried this code in js, but what i want is realtime response. ? https://i.stack.imgur.com/OkWj8.png

<script type="text/javascript">
        //<!--
        var image = 'http://www.hdwallpapers.in/walls/beautiful_makeup_girl_face_art-wide.jpg';
        var size = 5690894 ;//5MB
        var time1 = 0;
        var time2 = 0;
        function start_test_vitesse()
        {
            time1 = new Date();
            time1 = time1.getTime();
            var img = new Image();
            img.src = image+'?'+time1;
            img.onload=end_test_vitesse;
            img.onerror = imagenotfound;
            function imagenotfound() {
                console.log('sorry your internet is down');
                console.log(time1);
                time1=time1 * 100;
            }

            console.log(time1);

        }
        function end_test_vitesse()
        {
            var time2 = new Date();
            time2 = time2.getTime();
            var ms = time2-time1;
            var vitesse = Math.round(size/ms*100)/100;
            document.getElementById('vitesse').value=vitesse+'ko/s';
        }
        //-->
    </script> 
camjocotem
  • 375
  • 5
  • 18
DonMehdi
  • 21
  • 3
  • the problem is i dont want to have the speed after the file is loaded. i you take utorrent for example, we have the download speed before the file is downloaded. I hope you understand what i mean. thnks – DonMehdi Apr 01 '16 at 12:02
  • The only way to do this is to download something and measure the time it took to download. BT is simply doing this on an ongoing basis, measuring how much time the last chuck took to download/measure how many chunks/bytes it can download in one second. BT cannot give you statistics *immediately*, it takes at least one second after the start of the download for it to give you statistics for the previous second. If you download a small enough file, the effect is the same. – deceze Apr 01 '16 at 12:54
  • but how can you explain whats in this image even know that the file isnt fully downloaded. http://i.imgur.com/APeXWLI.png – DonMehdi Apr 01 '16 at 12:56
  • 1
    Native applications have a much lower level access to what's going on on the network; they *can* measure the data throughput within a specific timeframe. Javascript off the top of my head doesn't have that kind of capability, it only exposes a very high level API for you to work with. – deceze Apr 01 '16 at 13:00
  • So what could be the solution to have such a result ? – DonMehdi Apr 01 '16 at 13:03
  • You could create a persistent cookie for your users, and update it with the last known download rate from that user. If you have average download rates from the server, you could use that value as a default. – phatfingers Apr 05 '22 at 15:27

0 Answers0