Sample code for 30+ languages & platforms
Visual FoxPro

Write Bytes to a Remote SFTP File (BinData)

See more SFTP Examples

Demonstrates the Chilkat SFtp.WriteFileBd method, which appends all bytes in a BinData to the remote file identified by an open handle. The first argument is the handle and the second is the BinData.

Note: This example uses a relative local path, which is resolved against the application's current working directory. Absolute local paths may also be used. Supply the path appropriate to your own environment.

Background: This is the binary counterpart to WriteFileText — the right choice for images, archives, or any non-text content, where a charset conversion would corrupt the data. Combined with an appropriate OpenFile disposition it can create, overwrite, or append; here the file is created/truncated first so the result is exactly the BinData contents.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSftp
LOCAL lnPort
LOCAL lcPassword
LOCAL lcHandle
LOCAL loBd

lnSuccess = 0

*  Demonstrates the SFtp.WriteFileBd method, which appends all bytes in a BinData to the remote
*  file identified by an open handle.  The 1st argument is the handle and the 2nd is the BinData.

loSftp = CreateObject('Chilkat.SFtp')

*  Connect, authenticate, and initialize the SFTP subsystem.
lnPort = 22
lnSuccess = loSftp.Connect("sftp.example.com",lnPort)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
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.
lcPassword = "mySshPassword"

lnSuccess = loSftp.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

lnSuccess = loSftp.InitializeSftp()
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

*  Open (creating/truncating) the remote file for writing.
lcHandle = loSftp.OpenFile("subdir/image.png","writeOnly","createTruncate")
IF (loSftp.LastMethodSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

*  Put the bytes to write into a BinData.  (Here they are loaded from a local file, but the
*  bytes could come from anywhere.)
loBd = CreateObject('Chilkat.BinData')
lnSuccess = loBd.LoadFile("qa_data/image.png")
IF (lnSuccess = 0) THEN
    ? loBd.LastErrorText
    RELEASE loSftp
    RELEASE loBd
    CANCEL
ENDIF

*  Append the BinData bytes to the open remote file.
lnSuccess = loSftp.WriteFileBd(lcHandle,loBd)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    RELEASE loBd
    CANCEL
ENDIF

lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    RELEASE loBd
    CANCEL
ENDIF

? "Wrote " + STR(loBd.NumBytes) + " bytes."

loSftp.Disconnect()

RELEASE loSftp
RELEASE loBd