Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
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 sbA = CkStringBuilder();
  sbA.loadFile('qa_data/hamlet.xml', 'utf-8');

  // Upload the contents of sbA to the SSH/SFTP server.
  final bIncludeBOM = false;
  final remotePath = 'sftpTesting/hamlet.xml';
  success = true;
  try {
    sftp.uploadSb(sbA, remotePath, 'utf-8', bIncludeBOM);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(sftp.lastErrorText);
    return;
  }

  // Download the file..
  final sbB = CkStringBuilder();
  success = true;
  try {
    sftp.downloadSb(remotePath, 'utf-8', sbB);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(sftp.lastErrorText);
    return;
  }

  // Verify that sbA and sbB have the exact same contents.
  print('size of sbA: ${sbA.length}');
  final bCaseSensitive = true;
  if (sbA.contentsEqualSb(sbB, bCaseSensitive)) {
    print('Contents are equal. Success.');
  } else {
    print('Contents are NOT equal.  Failed.');
  }

  sftp.disconnect();
}