React Native
React Native
FTP Upload / Download to StringBuilder
Demonstrate how to upload from a Chilkat StringBuilder object, and download into a StringBuilder object.Chilkat React Native Downloads
import { Ftp2, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
const ftp = new Ftp2();
ftp.hostname = 'www.my-ftp-server.com';
ftp.username = 'mFtpLogin';
ftp.password = 'myFtpPassword';
// Connect to the FTP server.
try {
await ftp.connectOnlyAsync();
} catch {
console.log(ftp.lastErrorText);
return;
}
// Authenticate with the FTP server.
try {
await ftp.loginAfterConnectOnlyAsync();
} catch {
console.log(ftp.lastErrorText);
return;
}
const sbA = new StringBuilder();
sbA.loadFile('qa_data/hamlet.xml', 'utf-8');
// Upload the contents of sbA to the FTP server.
const bIncludeBOM = false;
const remoteFilename = 'hamletFromSb.xml';
try {
await ftp.putFileSbAsync(sbA, 'utf-8', bIncludeBOM, remoteFilename);
} catch {
console.log(ftp.lastErrorText);
return;
}
// Download...
const sbB = new StringBuilder();
try {
await ftp.getFileSbAsync(remoteFilename, 'utf-8', sbB);
} catch {
console.log(ftp.lastErrorText);
return;
}
// Verify that sbA and sbB have the exact same contents.
console.log(`size of sbA: ${sbA.length}`);
const bCaseSensitive = true;
if (sbA.contentsEqualSb(sbB, bCaseSensitive)) {
console.log('Contents are equal. Success.');
} else {
console.log('Contents are NOT equal. Failed.');
}
await ftp.disconnectAsync();
}