Dart
Dart
SFTP Write Text File
See more SFTP Examples
Demonstrates how to create a new text file on the remote SSH server, append text by calling WriteFileText one or more times, and then close the file.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes 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 = 15000;
// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
final hostname = 'sftp.example.com';
final port = 22;
try {
sftp.connect(hostname, port);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Authenticate with the SSH server. Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication. This example uses password authenication.
try {
sftp.authenticatePw('myLogin', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// After authenticating, the SFTP subsystem must be initialized:
try {
sftp.initializeSftp();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Open a file on the server for writing.
// "createTruncate" means that a new file is created; if the file already exists, it is opened and truncated.
final String handle;
try {
handle = sftp.openFile('myTest.txt', 'writeOnly', 'createTruncate');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Write some text to the file:
try {
sftp.writeFileText(handle, 'ansi', 'abcdefghijklmnopqrstuvwxyz');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
sftp.writeFileText(handle, 'ansi', '1234567890');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
sftp.writeFileText(handle, 'ansi', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Close the file.
try {
sftp.closeHandle(handle);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Success.');
}