Sample code for 30+ languages & platforms
PureBasic

SFTP Upload and Download to a BinData Object

See more SFTP Examples

Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Set some timeouts, in milliseconds:
    CkSFtp::setCkConnectTimeoutMs(sftp, 5000)
    CkSFtp::setCkIdleTimeoutMs(sftp, 10000)

    ; Connect to the SSH server and then authenticate.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"MY-SSH-SERVER-DOMAIN-OR-IP",port)
    If success = 1
        success = CkSFtp::ckAuthenticatePw(sftp,"MY-SSH-LOGIN","MY-SSH-PASSWORD")
        If success = 1
            success = CkSFtp::ckInitializeSftp(sftp)
        EndIf

    EndIf

    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    bdA.i = CkBinData::ckCreate()
    If bdA.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkBinData::ckLoadFile(bdA,"qa_data/jpg/penguins.jpg")

    ; Upload the contents of bdA to the SSH/SFTP server.
    remotePath.s = "sftpTesting/penguins.jpg"
    success = CkSFtp::ckUploadBd(sftp,bdA,remotePath)
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkBinData::ckDispose(bdA)
        ProcedureReturn
    EndIf

    ; Download the file..
    bdB.i = CkBinData::ckCreate()
    If bdB.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSFtp::ckDownloadBd(sftp,remotePath,bdB)
    If success <> 1
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkBinData::ckDispose(bdA)
        CkBinData::ckDispose(bdB)
        ProcedureReturn
    EndIf

    ; Verify that bdA and bdB have the exact same contents.
    Debug "size of bdA: " + Str(CkBinData::ckNumBytes(bdA))
    If CkBinData::ckContentsEqual(bdA,bdB) = 1
        Debug "Contents are equal. Success."
    Else
        Debug "Contents are NOT equal.  Failed."
    EndIf

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)
    CkBinData::ckDispose(bdA)
    CkBinData::ckDispose(bdB)


    ProcedureReturn
EndProcedure