Sample code for 30+ languages & platforms
PowerBuilder

Download an FTP File to a Stream

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetFileToStream method, which downloads a remote file and writes its bytes to a Stream. The first argument is the remote file path and the second is the Stream, which must have a destination sink configured beforehand.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Streaming to a Stream object decouples the download from where the bytes ultimately go — a file (as here, via SinkFile), or a pipe feeding another component such as a decompressor or a hashing routine — and processes the data as it arrives rather than buffering the whole file. That keeps memory flat for large downloads and lets you chain the transfer into a processing pipeline.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ftp
oleobject loo_FileStream

li_Success = 0

//  Demonstrates the Ftp2.GetFileToStream method, which downloads a remote file and writes its
//  bytes to a Stream.  The 1st argument is the remote file path and the 2nd is the Stream.

loo_Ftp = create oleobject
li_rc = loo_Ftp.ConnectToNewObject("Chilkat.Ftp2")
if li_rc < 0 then
    destroy loo_Ftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Ftp.Hostname = "ftp.example.com"
loo_Ftp.Username = "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.
loo_Ftp.Password = "myPassword"

li_Success = loo_Ftp.Connect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

//  Configure the stream's destination sink before downloading.  Here the stream writes to a
//  local file.
loo_FileStream = create oleobject
li_rc = loo_FileStream.ConnectToNewObject("Chilkat.Stream")

loo_FileStream.SinkFile = "qa_output/report.pdf"

li_Success = loo_Ftp.GetFileToStream("public_html/report.pdf",loo_FileStream)
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    destroy loo_FileStream
    return
end if

Write-Debug "Downloaded to stream."

li_Success = loo_Ftp.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    destroy loo_FileStream
    return
end if



destroy loo_Ftp
destroy loo_FileStream