Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

// 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.

let ftp = chilkat::Ftp2::new();

ftp.set_hostname("ftp.example.com");
ftp.set_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.
ftp.set_password("myPassword");

if ftp.connect().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Build the text to upload.
let sb = chilkat::StringBuilder::new();
let _ = sb.append("name,quantity\n");
let _ = sb.append("widgets,42\n");

// Upload as UTF-8 without a byte-order mark.
let include_bom = false;
if ftp.put_file_sb(&sb, "utf-8", include_bom, "public_html/inventory.csv").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

println!("Uploaded text file.");

if ftp.disconnect().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}