Sample code for 30+ languages & platforms
PureBasic

FTP Upload / Download to a BinData Object

Demonstrates how to FTP upload the contents of a BinData object, and FTP downloads to the a BinData object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example assumes Chilkat Ftp2 to have been previously unlocked.
    ; See Unlock Ftp2 for sample code.

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

    CkFtp2::setCkHostname(ftp, "www.my-ftp-server.com")
    CkFtp2::setCkUsername(ftp, "mFtpLogin")
    CkFtp2::setCkPassword(ftp, "myFtpPassword")

    ; Connect to the FTP server.
    success = CkFtp2::ckConnectOnly(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Authenticate with the FTP server.
    success = CkFtp2::ckLoginAfterConnectOnly(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        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 FTP server.
    remoteFilename.s = "penguins.jpg"
    success = CkFtp2::ckPutFileBd(ftp,bdA,remoteFilename)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkBinData::ckDispose(bdA)
        ProcedureReturn
    EndIf

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

    success = CkFtp2::ckGetFileBd(ftp,remoteFilename,bdB)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        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

    CkFtp2::ckDisconnect(ftp)


    CkFtp2::ckDispose(ftp)
    CkBinData::ckDispose(bdA)
    CkBinData::ckDispose(bdB)


    ProcedureReturn
EndProcedure