How to monitor the status of a download request made using tsNetGetSync

Introduction

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.

Lay out the Stack

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.

Create the Script

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 and pTotalDown is not 0 then
      set the thumbPosition of scrollbar "Progress Scrollbar" to (pBytesDown / pTotalDown) * 100
   end if
end showProgress

Test

Switch to Run mode and click the button.

More information

As can be seen in the example code above, the handler set by the tsNetSetStatusCallback command is called with six parameters:

  1. pID - the connection identifier for the transfer that the status update relates to.  For synchronous transfers, this will be the URL of the transfer.
  2. 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.
  3. pBytesDown - the number of bytes that have been downloaded by the transfer.
  4. pTotalDown - the total number of bytes that are to be downloaded.
  5. pBytesUp - the number of bytes that been uploaded by the transfer.
  6. pTotalUp - the total number of bytes that are to be uploaded.

 

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.