React Native
React Native
SFTP Upload and Download to a BinData Object
See more SFTP Examples
Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.Chilkat React Native Downloads
import { BinData, SFtp } 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 bdA = new BinData();
bdA.loadFile('qa_data/jpg/penguins.jpg');
// Upload the contents of bdA to the SSH/SFTP server.
const remotePath = 'sftpTesting/penguins.jpg';
success = true;
try {
await sftp.uploadBdAsync(bdA, remotePath);
} catch {
success = false;
}
if (!success) {
console.log(sftp.lastErrorText);
return;
}
// Download the file..
const bdB = new BinData();
success = true;
try {
await sftp.downloadBdAsync(remotePath, bdB);
} catch {
success = false;
}
if (!success) {
console.log(sftp.lastErrorText);
return;
}
// Verify that bdA and bdB have the exact same contents.
console.log(`size of bdA: ${bdA.numBytes}`);
if (bdA.contentsEqual(bdB)) {
console.log('Contents are equal. Success.');
} else {
console.log('Contents are NOT equal. Failed.');
}
sftp.disconnect();
}