OverviewPropertiesAdvancedExamples
This example shows how you can use the Progress Bars screen to show progress for a file download that may take a long time to complete.
This example requires you to paste code onto three different action events on a Progress Bars screen. In order to test this example, you also need to have a file available for download.
To try this example, paste the following code onto the On Preload event of a Progress Bars screen. This event can be found on the Actions tab of the screen's properties.
-- Set some variables for the file download...
-- Put your full URL to the file here...
strSourceURL = "http://www.yoursite.com/myfile.zip";
-- Put the destination path and filename here...
strDestinationFile = _TempFolder.."\\myfile.zip";
bIsBinaryFile = true;
-- Set to true if the user presses cancel (see the On Cancel event)
bCancelled = false;
-- Initialize the progress bar...
DlgProgressBar.SetRange(CTRL_PROGRESS_BAR_01,0,100);
DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,0);
This action script sets some variables that are required for the rest of the screen's action script. You must make some modifications to its script such as the file's source and destination.
Paste the following code onto the On Start event of the Progress Bars screen. This event can be found on the Actions tab of the screen's properties.
-- Initialize some display variables...
local strLocalFileName = strDestinationFile;
local tblFileParts = String.SplitPath(strDestinationFile);
if(tblFileParts)then
strLocalFileName = tblFileParts.Filename..tblFileParts.Extension;
end
local strWebSiteName = strSourceURL;
local nPosition = String.Find(strSourceURL,"://");
if(nPosition ~= -1)then
local nPositionNextSlash = String.Find(strSourceURL,"/",nPosition+3);
if(nPositionNextSlash ~= -1)then
strWebSiteName = String.Mid(strSourceURL,nPosition+3,(nPositionNextSlash-nPosition-3));
end
end
-- The callback function - used to display the download progress
function fcnDownloadCallback(BytesRead,FileSize,TransferRate,SecondsLeft,SecondsLeftFormat,Message)
if(bCancelled)then
-- Abort the download...
return false;
end
DlgStaticText.SetProperties(CTRL_STATICTEXT_LABEL_01,{Text="Downloading: "..strLocalFileName.." from "..strWebSiteName});
local nPctComplete = 0;
if(FileSize > 0)then
nPctComplete = (BytesRead/FileSize) * 100;
end
DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,nPctComplete);
local strStatusText = "";
if(Message ~= "")then
strStatusText = Message;
else
-- It is downloading...
local strXferRate = string.format("%.2f",TransferRate);
strStatusText = "Estimated time left: "..SecondsLeftFormat.." - Transfer Rate: "..strXferRate.." KB/Sec";
end
DlgStaticText.SetProperties(CTRL_STATICTEXT_LABEL_02,{Text=strStatusText});
return true;
end
local nTransferType = MODE_TEXT;
if(bIsBinaryFile)then
nTransferType = MODE_BINARY;
end
-- The actual download action...
HTTP.Download(strSourceURL, strDestinationFile, nTransferType, 20, 80, nil, nil, fcnDownloadCallback);
local nLastError = Application.GetLastError();
if(nLastError ~= 0)then
Dialog.Message("Download Failed", _tblErrorMessages[nLastError]);
end
This action script uses the HTTP.Download action to download a file and displays its progress using the Progress Bars screen. The action's visible progress and status is handled by the callback function fcnDownloadCallback containing actions such as DlgProgressBar.SetPos
Paste the following code onto the On Cancel event of the Progress Bars screen. This event can be found on the Actions tab of the screen's properties.
local nResult = Dialog.Message("Confirm","Are you sure that you want to abort the download?",MB_YESNO,MB_ICONQUESTION);
if(nResult == IDYES)then
bCancelled = true;
end
This action script handles the case where the user pressed the Cancel button on the screen. If this occurs, a dialog message will be shown and the action will be cancelled if the user presses "Yes".