LiveCode LessonsLiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCodeTSNetHow to asynchronously download via SFTP directly to a file

How to asynchronously download via SFTP directly to a file


In a similar fashion to many other protocols, the tsNet external supports transferring files over SFTP.

In this lesson, we look at how to download a file asynchronously over SFTP without storing the contents of the file in a variable first.

Please note that this lesson requires the Business Edition of LiveCode.


Lay out the Stack

Create a new stack then drag a button and a field onto it.

Set the name of this field to "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 pMouseButton
   -- Store the SFTP server details in some variables
   put "s" into tSFTPServer
   put "/pub/example/ResumableTransfer.png" into tSFTPFileToDownload
   put specialFolderPath("documents") & slash & "ResumableTransfer.png" into tLocalPath
   -- The tSettings array can be used to provide additional configuration details, in this case
   -- the username and password to use when downloading the file via SFTP
   put "demo" into tSettings["username"]
   put "password" into tSettings["password"]
   -- Note that when using tsNetGetFile we must specify a connection ID (any arbitrary value) and a handler to 
   -- call when the transfer is completed, in this case we want it to call "tsNetGetFileFinished".
   put tsNetGetFile("mySFTPdownload", tLocalPath, tSFTPServer & tSFTPFileToDownload, tHeaders, "tsNetGetFileFinished", tSettings) into tResult
   -- If tResult contains anything at this point, there is an error in the parameters sent to the tsNetGet call.
   if tResult is not empty then
      -- Inform the user of the error
      put "Error in tsNetGetFile call:" && tResult into field "Status"
      -- tResult must be empty at this point, so let the user know the call has been successful.
      put "tsNetGetFile has been issued, awaiting response" into field "Status"
   end if
end mouseUp

on tsNetGetFileFinished pID, pResult, pBytes, pCurlCode
   put cr & "tsNetGetFile request has completed, checking result" after field "Status"
   -- If pCurlCode is anything other than 0, the request has not been successful.
   -- pResult will also be 0 for successful SFTP transfers
   -- If either of these are not the case, we will inform the user
   if pCurlCode is not 0 or pResult is not 0 then 
      put cr & "Could not retrieve data from site" after field "Status"
      -- The file has been downloaded and stored in the location indicated by the tLocalPath variable
      put cr & "File has downloaded successfully" after field "Status"
   end if 
   -- Tidy up the resources used by the tsNetGetFile call:
   tsNetCloseConn pID
end tsNetGetFileFinished
Click to copy

This example is sample code. We recommend never putting usernames and passwords in scripts you distribute (they should be collected from the user!).


Switch to Run mode and click the button.

More information

Any transfer performed with tsNet over the SFTP protocol will always return a server status code of 0.  Please note that this is not the case with HTTP and FTP protocols as they will return a variety of status codes.

The best way to check for any errors in the callback function when using SFTP is to check the pCurlCode parameter.  A successful transfer will always result in this parameter being 0.


Dave Kilroy

Hi Charles - is it possible to send a call to download multiple files asynchronously with this? If not what tsNet command would you suggest? Thanks, Dave

Charles Warwick

Hi Dave,

Sorry for the late reply. You can use the tsNetGetFile function to achieve this. For an example of how to do this, please refer to "How to download multiple files in the background with tsNet". I hope this helps.

Best Regards,

Add your comment

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