React Native
React Native
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 React Native Downloads
import { SFtp, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
let success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const sftp = new SFtp();
// Set some timeouts, in milliseconds:
sftp.connectTimeoutMs = 5000;
sftp.idleTimeoutMs = 10000;
// Connect to the SSH server and then authenticate.
const port = 22;
success = true;
try {
await sftp.connectAsync('MY-SSH-SERVER-DOMAIN-OR-IP', port);
} catch {
success = false;
}
if (success) {
success = true;
try {
await sftp.authenticatePwAsync('MY-SSH-LOGIN', 'MY-SSH-PASSWORD');
} catch {
success = false;
}
if (success) {
success = true;
try {
await sftp.initializeSftpAsync();
} catch {
success = false;
}
}
}
if (!success) {
console.log(sftp.lastErrorText);
return;
}
const sbA = new StringBuilder();
sbA.loadFile('qa_data/hamlet.xml', 'utf-8');
// Upload the contents of sbA to the SSH/SFTP server.
const bIncludeBOM = false;
const remotePath = 'sftpTesting/hamlet.xml';
success = true;
try {
await sftp.uploadSbAsync(sbA, remotePath, 'utf-8', bIncludeBOM);
} catch {
success = false;
}
if (!success) {
console.log(sftp.lastErrorText);
return;
}
// Download the file..
const sbB = new StringBuilder();
success = true;
try {
await sftp.downloadSbAsync(remotePath, 'utf-8', sbB);
} catch {
success = false;
}
if (!success) {
console.log(sftp.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.');
}
sftp.disconnect();
}