DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
String sHandle
Variant vBd
Handle hoBd
Integer iChunkSize
Boolean iReading
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComOpenFile Of hoSftp "subdir/image.png" "readOnly" "openExisting" To sHandle
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Read the file in chunks until end-of-file, accumulating the bytes in a BinData.
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Move 8192 To iChunkSize
Move True To iReading
While iReading
Get pvComObject of hoBd to vBd
Get ComReadFileBd Of hoSftp sHandle iChunkSize vBd To iSuccess
Get ComLastReadFailed Of hoSftp sHandle To bTemp1
If (bTemp1) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComEof Of hoSftp sHandle To bTemp1
Move (Not bTemp1) To iReading
Loop
Get ComCloseHandle Of hoSftp sHandle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumBytes Of hoBd To iTemp1
Showln "Read " iTemp1 " bytes."
Send ComDisconnect To hoSftp
End_Procedure