ftpconnect 3.0.0
ftpconnect: ^3.0.0 copied to clipboard
Simple and robust dart FTP Connect Library to interact with FTP Servers.
import 'dart:io';
import 'package:ftpconnect/ftpconnect.dart';
void main() async {
final FTPConnect ftpConnect = FTPConnect(
"users.on.net",
user: "pvpt",
pass: "Lachdhaf",
showLog: true,
);
///an auxiliary function that manage showed log to UI
Future<void> log(String log) async {
print(log);
await Future.delayed(Duration(seconds: 1));
}
///mock a file for the demonstration example
Future<File> fileMock({fileName = 'FlutterTest.txt', content = ''}) async {
final Directory directory = Directory('/test')..createSync(recursive: true);
final File file = File('${directory.path}/$fileName');
await file.writeAsString(content);
return file;
}
Future<void> uploadStepByStep() async {
try {
await log('Connecting to FTP ...');
await ftpConnect.connect();
await ftpConnect.changeDirectory('upload');
File fileToUpload = await fileMock(
fileName: 'uploadStepByStep.txt', content: 'uploaded Step By Step');
await log('Uploading ...');
await ftpConnect.uploadFile(fileToUpload);
await log('file uploaded sucessfully');
await ftpConnect.disconnect();
} catch (e) {
await log('Error: ${e.toString()}');
}
}
Future<void> downloadStepByStep() async {
try {
await log('Connecting to FTP ...');
await ftpConnect.connect();
await log('Downloading ...');
String fileName = '../512KB.zip';
//here we just prepare a file as a path for the downloaded file
File downloadedFile = await fileMock(fileName: 'downloadStepByStep.txt');
await ftpConnect.downloadFile(fileName, downloadedFile);
await log('file downloaded path: ${downloadedFile.path}');
await ftpConnect.disconnect();
} catch (e) {
await log('Downloading FAILED: ${e.toString()}');
}
}
await uploadStepByStep();
await downloadStepByStep();
}