How to monitor the status of a download request made using tsNetGetSync
It is often useful to be able to display the progress of a download within an application to the user.
The tsNet external provides a command tsNetSetStatusCallback that allows you to specify a LC handler that will be regularly sent messages with the current status of any transfers in progress.
This lesson shows how you can use this command to display a progress bar to the user when a download is occuring.
Create a new stack then drag a button, progress scrollbar and a field onto it.
Set the name of the field to "Download Status".
Save this stack.
Edit the script of the button that you placed on the stack by right clicking on the button and selecting "edit script".
Add the following code.
on mouseUp set the thumbPosition of scrollbar "Progress Scrollbar" to 0 tsNetSetStatusCallback "showProgress" put tsNetGetSync("https://downloads.techstrategies.com.au/20mb.file", tHeaders, tRecvHeaders, tResult, tBytes) into tData end mouseUp on showProgress pID, pStatus, pBytesDown, pTotalDown, pBytesUp, pTotalUp put pStatus into field "Download Status" if pTotalDown is not empty then set the thumbPosition of scrollbar "Progress Scrollbar" to (pBytesDown / pTotalDown) * 100 end if end showProgress
As can be seen in the example code above, the handler set by the tsNetSetStatusCallback command is called with six parameters:
- pID - the connection identifier for the transfer that the status update relates to. For synchronous transfers, this will be the URL of the transfer.
- pStatus - a single word that describes the current status of the transfer. This can be one of the following: contacted, requested, loading, uploading, downloaded, uploaded, timeout or error.
- pBytesDown - the number of bytes that have been downloaded by the transfer.
- pTotalDown - the total number of bytes that are to be downloaded.
- pBytesUp - the number of bytes that been uploaded by the transfer.
- pTotalUp - the total number of bytes that are to be uploaded.