Sample code for 30+ languages & platforms
Tcl

Upload Bytes to an FTP Server (BinData)

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutFileBd method, which uploads the bytes in a BinData to a remote path. The first argument is the BinData and the second is the remote file path. No encoding conversion is performed.

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: When the content already lives in memory — generated, decompressed, or pulled from a database — this uploads it directly with no intermediate local file. It uses STOR, so an existing remote file is replaced, and the bytes are sent exactly as held, making it the correct choice for binary data.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Ftp2.PutFileBd method, which uploads the bytes in a BinData to a remote path.
#  The 1st argument is the BinData and the 2nd is the remote file path.  No encoding conversion is
#  performed.

set ftp [new_CkFtp2]

CkFtp2_put_Hostname $ftp "ftp.example.com"
CkFtp2_put_Username $ftp "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.
CkFtp2_put_Password $ftp "myPassword"

set success [CkFtp2_Connect $ftp]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    exit
}

#  Put the bytes to upload into a BinData.  (Here they are loaded from a local file, but the
#  bytes could come from anywhere.)
set bd [new_CkBinData]

set success [CkBinData_LoadFile $bd "qa_data/image.png"]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bd]
    delete_CkFtp2 $ftp
    delete_CkBinData $bd
    exit
}

set success [CkFtp2_PutFileBd $ftp $bd "public_html/image.png"]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    delete_CkBinData $bd
    exit
}

puts "Uploaded [CkBinData_get_NumBytes $bd] bytes."

set success [CkFtp2_Disconnect $ftp]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    delete_CkBinData $bd
    exit
}


delete_CkFtp2 $ftp
delete_CkBinData $bd