Swift
Swift
FTP Upload / Download to StringBuilder
Demonstrate how to upload from a Chilkat StringBuilder object, and download into a StringBuilder object.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
let ftp = CkoFtp2()!
ftp.hostname = "www.my-ftp-server.com"
ftp.username = "mFtpLogin"
ftp.password = "myFtpPassword"
// Connect to the FTP server.
success = ftp.connectOnly()
if success != true {
print("\(ftp.lastErrorText!)")
return
}
// Authenticate with the FTP server.
success = ftp.loginAfterConnectOnly()
if success != true {
print("\(ftp.lastErrorText!)")
return
}
let sbA = CkoStringBuilder()!
sbA.loadFile(path: "qa_data/hamlet.xml", charset: "utf-8")
// Upload the contents of sbA to the FTP server.
var bIncludeBOM: Bool = false
var remoteFilename: String? = "hamletFromSb.xml"
success = ftp.putFileSb(sb: sbA, charset: "utf-8", includeBom: bIncludeBOM, remoteFilePath: remoteFilename)
if success != true {
print("\(ftp.lastErrorText!)")
return
}
// Download...
let sbB = CkoStringBuilder()!
success = ftp.getFileSb(remoteFilePath: remoteFilename, charset: "utf-8", sb: sbB)
if success != true {
print("\(ftp.lastErrorText!)")
return
}
// Verify that sbA and sbB have the exact same contents.
print("size of sbA: \(sbA.length.intValue)")
var bCaseSensitive: Bool = true
if sbA.contentsEqualSb(sb: sbB, caseSensitive: bCaseSensitive) == true {
print("Contents are equal. Success.")
}
else {
print("Contents are NOT equal. Failed.")
}
ftp.disconnect()
}