Sample code for 30+ languages & platforms
Zig

FTP Upload with Progress Event Callbacks

See more FTP Examples

FTP upload with progress event callbacks.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

const FtpEvents = struct {
    pub fn percentDone(_: *FtpEvents, pct: i32) bool {
        std.debug.print("Percent Done: {d}\n", .{pct});
        return false; // return true to abort the method in progress
    }

    pub fn progressInfo(_: *FtpEvents, name: [:0]const u8, value: [:0]const u8) void {
        std.debug.print("{s}: {s}\n", .{ name, value });
    }
};

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();
    var ftp_events = FtpEvents{};
    ftp.setEventHandler(&ftp_events);

    ftp.setHostname("ftp.someFtpServer.com");
    ftp.setUsername("my_ftp_username");
    ftp.setPassword("my_ftp_password");

    // Connect and login to the FTP server.
    ftp.connect() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Change to the remote directory where the file will be uploaded.
    ftp.changeRemoteDir("junk") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Upload a file.
    const local_path = "c:/temp/hamlet.xml";
    const remote_filename = "hamlet.xml";

    ftp.putFile(local_path, remote_filename) catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    try ftp.disconnect();

    std.debug.print("File Uploaded!\n", .{});
}