Sample code for 30+ languages & platforms
PureBasic

Read Bytes from a Remote SFTP File (BinData)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileBd method, which reads up to a number of bytes from the current position of an open handle and appends them to a BinData. The arguments are the handle, the maximum number of bytes, and the BinData. This example reads in chunks until end-of-file.

Background: For binary files, reading into a BinData preserves the bytes exactly, unlike a text read that decodes through a charset. Because it appends, the chunked read loop accumulates the whole file in the BinData; each pass reads up to the chunk size and the loop repeats until Eof. Fewer bytes than requested can come back near the end of the file, which is normal.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SFtp.ReadFileBd method, which reads up to a number of bytes from the current
    ;  position of an open handle and appends them to a BinData.  The 1st argument is the handle, the
    ;  2nd is the maximum number of bytes, and the 3rd is the BinData.

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

    ;  Connect, authenticate, and initialize the SFTP subsystem.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "mySshPassword"

    success = CkSFtp::ckAuthenticatePw(sftp,"mySshLogin",password)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    handle.s = CkSFtp::ckOpenFile(sftp,"subdir/image.png","readOnly","openExisting")
    If CkSFtp::ckLastMethodSuccess(sftp) = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Read the file in chunks until end-of-file, accumulating the bytes in a BinData.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    chunkSize.i = 8192
    reading.i = 1
    While reading
        success = CkSFtp::ckReadFileBd(sftp,handle,chunkSize,bd)
        If CkSFtp::ckLastReadFailed(sftp,handle)
            Debug CkSFtp::ckLastErrorText(sftp)
            CkSFtp::ckDispose(sftp)
            CkBinData::ckDispose(bd)
            ProcedureReturn
        EndIf

        reading = Not CkSFtp::ckEof(sftp,handle)
    Wend

    success = CkSFtp::ckCloseHandle(sftp,handle)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Read " + Str(CkBinData::ckNumBytes(bd)) + " bytes."

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure