Bayesean Blog - Desktop, Mobile and IOT Developer Blog


PHP Downloader using Countdown timer

Posted on 17th Mar 2017 in Bootstrap 4, CSS


timeddownloader.gif

This is an update of the original blog updated from bootstrap 3 to use bootstrap 4 and with a smooth progress bar adjusted in the JavaScript code. The viewer has been removed and replaced with the above gif.

 

Project Scope

This project scope came about when a comment was received asking how to use a seconds timed delay. So I put a blog together on this. The original progress bar was  jerky in nature and did not look professional.

While updating the blog to Bootstrap 4, the scope required a smooth progressbar showing the seconds counting down.

  • Must not show the download button immediately to prevent non-human downloading.
  • Must keep the user knowledgeable as to what is happening by adding a count-down timer linked to a progress bar.
  • Must flow automatically.
  • Must immediately download the file that was selected on the click of the button that became visible after completion of the progress bar.

Coding Difficulty

Medium. Good knowledge of Javascript/Ajax and PHP required would help.

Libraries

Bootstrap 4 Framework –download  here.

PHP 5.6 +   download  here .

Downloader.php  -  A PHP Library (A  free to use file download library created by Jack Mason )

This is included in the included Download below.

JQuery – download from here or visit the website http://jquery.com.

Javascript.

 

Structure

Index.php

This file contains the visual element of a drop down box to select the file to download.

Download2.php

This file contains the visual download progress bar and button to download the file.

Downloader1.php

This file is called by the download button and processes the file to download.

Downloader.php

This holds all the mime information and allows any type of file to be downloaded.

Code

To show and hide classes, the class “hidden” is no longer for bootstrap 4. This is replaced with class = ”d-none” to perform the same functionality.

The main action in the blog is within the interval of the JavaScript code. To use seconds change for interval timing results in an un-smooth progress bar change. So the trick is to update the width of the progress bar at 10 times the speed of a second. This allows the progress bar to update smoothly. Should you try to update in milliseconds, the bootstrap 4 progress bar does not react correctly as it is not designed to update at that frequency.

Whilst the progress bar (width) is updating at 10 times the speed, the second value is updated when the ‘proglevel’ (check code below) value equals the value of a second then this triggers the title in the progress bar to update the seconds value.

To adjust the delay in seconds, change only the variable ‘seconds = 20’ to for example ‘seconds = 40’.

Herewith is a snippet of the main JavaScript function that controls the progress bar’s changes.

You can download the full code for the project at the end of the blog

 

<script type="text/javascript">
     var countdownElement = document.getElementById('progress-bar'),
      seconds = 20, // changes the seconds delay in the progress bar
      second = 0,
      interval;
      proglevels = seconds*10;
      proglevel = 0;

    interval = setInterval(function() {
   
    var  varinfo = 'Downloading in ' + (seconds - second) + ' seconds';
     width = (100/proglevels)*(proglevels-(proglevels-proglevel));
     document.getElementById("progress-bar").style.width=width+"%";
     document.getElementById("progress-bar").innerHTML =varinfo ;
     if (second >= seconds) {
           $("#myId").removeClass("d-none");
           $("#myimg").removeClass("d-none");
           var varinfo2 = " Click Download Button to Download the File";
             document.getElementById("progress-bar").innerHTML =varinfo2;
             clearInterval(interval);
        }
        proglevel++;
       if ((proglevel/10)== (second + 1)){ 
        second++;
  }
    },100);
       </script>

 

License

 

Licence is MIT.

 

Download the code from the GITHUB repository.

 

Download Code

 

 

 


Rod      Commented   2 years ago Reply

How can you make the progress bar smoother in progress.

admin      Replied    Moderator   2 years agoReply


Hi, Yes this is a problem. That is why the previous blog worked on a quick countdown not linked to time. Saying this,it would be possible to force the process to update every millisecond then build in a method to update the label every second. Try it and let me know.

Add a Comment

9+5

Recent News

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction
Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App
Delphi FMX Leaflet Plotter using OSM Maps
C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4

Archives

October 2018

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button