PureBasic
PureBasic
Resume an Interrupted FTP Transfer
See more FTP Examples
Demonstrates the RestartNext and PartialTransfer properties, which request that the next transfer resume from existing content and report whether a download was incomplete.
Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: For a large transfer over a flaky link, restarting from zero after a drop is wasteful. Setting
RestartNext makes the next download continue from the current local file size (and the next upload from the remote size); the flag is consumed by that one transfer and then reset. Afterward PartialTransfer tells you whether the download got only part of the file — the signal to set RestartNext again and resume once more.Chilkat PureBasic Downloads
IncludeFile "CkFtp2.pb"
Procedure ChilkatExample()
success.i = 0
ftp.i = CkFtp2::ckCreate()
If ftp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkFtp2::setCkHostname(ftp, "ftp.example.com")
CkFtp2::setCkUsername(ftp, "myFtpLogin")
; Normally you would not hard-code the password in source. You should instead obtain it
; from an interactive prompt, environment variable, or a secrets vault.
CkFtp2::setCkPassword(ftp, "myPassword")
success = CkFtp2::ckConnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; RestartNext requests that the NEXT transfer resume from existing content rather than starting
; over. It is consumed by that transfer and then automatically reset.
CkFtp2::setCkRestartNext(ftp, 1)
; Resume a partially-downloaded file. Because RestartNext is set, the download continues from
; the current local file size.
success = CkFtp2::ckGetFile(ftp,"public_html/bigfile.zip","qa_output/bigfile.zip")
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; PartialTransfer indicates whether the most recent download received some, but not all, of the
; remote file -- distinguishing a mid-transfer failure from other outcomes.
If CkFtp2::ckPartialTransfer(ftp)
Debug "Only part of the file was received; another resume may be needed."
Else
Debug "The file was fully received."
EndIf
success = CkFtp2::ckDisconnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndProcedure