PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Sftp
long li_Port
string ls_Password
string ls_Handle
oleobject loo_Bd
li_Success = 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.
loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
destroy loo_Sftp
MessageBox("Error","Connecting to COM object failed")
return
end if
// Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
// 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.
ls_Password = "mySshPassword"
li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
// Open (creating/truncating) the remote file for writing.
ls_Handle = loo_Sftp.OpenFile("subdir/image.png","writeOnly","createTruncate")
if loo_Sftp.LastMethodSuccess = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
// Put the bytes to write into a BinData. (Here they are loaded from a local file, but the
// bytes could come from anywhere.)
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Bd.LoadFile("qa_data/image.png")
if li_Success = 0 then
Write-Debug loo_Bd.LastErrorText
destroy loo_Sftp
destroy loo_Bd
return
end if
// Append the BinData bytes to the open remote file.
li_Success = loo_Sftp.WriteFileBd(ls_Handle,loo_Bd)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
destroy loo_Bd
return
end if
li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
destroy loo_Bd
return
end if
Write-Debug "Wrote " + string(loo_Bd.NumBytes) + " bytes."
loo_Sftp.Disconnect()
destroy loo_Sftp
destroy loo_Bd