Sample code for 30+ languages & platforms
PureBasic

Download an FTP File into a BinData

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetFileBd method, which downloads a remote file and appends its bytes to a BinData. The first argument is the remote file path and the second is the BinData. No decoding or line-ending conversion is performed.

Background: Downloading into memory avoids a temporary local file when you intend to hash, parse, or forward the bytes immediately. Because it appends, existing bytes in the BinData are preserved — handy for concatenating several remote files, but clear it first when you want only this one. Since no conversion is applied, this is the safe way to pull content regardless of the current transfer type — which defaults to binary (SetTypeBinary), the recommended setting for essentially every file, text included. The SetTypeAscii mode applies legacy CRLF translation and is rarely needed.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.GetFileBd method, which downloads a remote file and appends its bytes to
    ;  a BinData.  The 1st argument is the remote file path and the 2nd is the BinData.  No decoding
    ;  or line-ending conversion is performed.

    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

    ;  The BinData is not cleared automatically, so clear it first for replacement behavior.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFtp2::ckGetFileBd(ftp,"public_html/image.png",bd)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Downloaded " + Str(CkBinData::ckNumBytes(bd)) + " bytes into memory."

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf



    CkFtp2::ckDispose(ftp)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure