Sample code for 30+ languages & platforms
Lua

SFTP Upload and Download to a StringBuilder Object

See more SFTP Examples

Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local sftp = chilkat.SFtp.new()

-- Set some timeouts, in milliseconds:
sftp.connect_timeout_ms = 5000
sftp.idle_timeout_ms = 10000

-- Connect to the SSH server and then authenticate.
local port = 22
local success = sftp:connect("MY-SSH-SERVER-DOMAIN-OR-IP", port)
if success then
    success = sftp:authenticate_pw("MY-SSH-LOGIN", "MY-SSH-PASSWORD")
    if success then
        success = sftp:initialize_sftp()
    end
end

if not success then
    print(sftp.last_error_text)
    return
end

local sbA = chilkat.StringBuilder.new()
sbA:load_file("qa_data/hamlet.xml", "utf-8")

-- Upload the contents of sbA to the SSH/SFTP server.
local bIncludeBOM = false
local remotePath = "sftpTesting/hamlet.xml"
success = sftp:upload_sb(sbA, remotePath, "utf-8", bIncludeBOM)
if not success then
    print(sftp.last_error_text)
    return
end

-- Download the file..
local sbB = chilkat.StringBuilder.new()
success = sftp:download_sb(remotePath, "utf-8", sbB)
if not success then
    print(sftp.last_error_text)
    return
end

-- Verify that sbA and sbB have the exact same contents.
print("size of sbA: " .. sbA.length)
local bCaseSensitive = true
if sbA:contents_equal_sb(sbB, bCaseSensitive) then
    print("Contents are equal. Success.")
else
    print("Contents are NOT equal.  Failed.")
end

sftp:disconnect()