Sample code for 30+ languages & platforms
Tcl

Upload Text to an FTP Server (StringBuilder)

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads it to a remote path. The arguments are the StringBuilder, the charset, whether to include a byte-order mark, and the remote file path.

Background: The text-oriented upload: assemble a document — CSV, config, JSON — in a StringBuilder and the method handles encoding as it writes. The charset governs the bytes actually stored, and the byte-order-mark flag is useful when a consuming Windows application expects a BOM.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads
#  it to a remote path.  The 1st argument is the StringBuilder, the 2nd is the charset, the 3rd
#  selects whether a byte-order mark is included, and the 4th is the remote file path.

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
}

#  Build the text to upload.
set sb [new_CkStringBuilder]

CkStringBuilder_Append $sb "name,quantity\n"
CkStringBuilder_Append $sb "widgets,42\n"

#  Upload as UTF-8 without a byte-order mark.
set includeBom 0
set success [CkFtp2_PutFileSb $ftp $sb "utf-8" $includeBom "public_html/inventory.csv"]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    delete_CkStringBuilder $sb
    exit
}

puts "Uploaded text file."

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


delete_CkFtp2 $ftp
delete_CkStringBuilder $sb