Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final sftp = CkSFtp();
// Set some timeouts, in milliseconds:
sftp.connectTimeoutMs = 5000;
sftp.idleTimeoutMs = 10000;
// Connect to the SSH server and then authenticate.
final port = 22;
success = true;
try {
sftp.connect('MY-SSH-SERVER-DOMAIN-OR-IP', port);
} on ChilkatException {
success = false;
}
if (success) {
success = true;
try {
sftp.authenticatePw('MY-SSH-LOGIN', 'MY-SSH-PASSWORD');
} on ChilkatException {
success = false;
}
if (success) {
success = true;
try {
sftp.initializeSftp();
} on ChilkatException {
success = false;
}
}
}
if (!success) {
print(sftp.lastErrorText);
return;
}
final bdA = CkBinData();
bdA.loadFile('qa_data/jpg/penguins.jpg');
// Upload the contents of bdA to the SSH/SFTP server.
final remotePath = 'sftpTesting/penguins.jpg';
success = true;
try {
sftp.uploadBd(bdA, remotePath);
} on ChilkatException {
success = false;
}
if (!success) {
print(sftp.lastErrorText);
return;
}
// Download the file..
final bdB = CkBinData();
success = true;
try {
sftp.downloadBd(remotePath, bdB);
} on ChilkatException {
success = false;
}
if (!success) {
print(sftp.lastErrorText);
return;
}
// Verify that bdA and bdB have the exact same contents.
print('size of bdA: ${bdA.numBytes}');
if (bdA.contentsEqual(bdB)) {
print('Contents are equal. Success.');
} else {
print('Contents are NOT equal. Failed.');
}
sftp.disconnect();
}