-
-
Notifications
You must be signed in to change notification settings - Fork 35
Description
FTPConnect ftpConnect=await FTPConnect('bmw.eyebase.com',user: 'testuser',pass:'aq4Toec4CTU$',securityType: SecurityType.FTPES,logger: Logger(isEnabled: true),port: 21 );
Hello I am unable to Upload to FTPES using upload method I also tired modiying the code.
Future upload(
File fFile, {
String remoteName = '',
FileProgress? onProgress,
}) async {
_socket.logger.log('Upload File: ${fFile.path}');
// Enter passive mode
FTPReply response = await _socket.openDataTransferChannel();
// Store File
String sFilename = remoteName;
if (sFilename.isEmpty) {
sFilename = basename(fFile.path);
}
// The response is the file to upload, witch will be managed by another socket
_socket.sendCommandWithoutWaitingResponse('STOR $sFilename');
// Data Transfer Socket
int iPort = Utils.parsePort(response.message, _socket.supportIPV6);
_socket.logger.log('Opening DataSocket to Port $iPort');
final Socket dataSocket = await Socket.connect(_socket.host, iPort);
// _socket.setSocket(dataSocket);
final SecureSocket secureDataSocket=await SecureSocket.secure(dataSocket, onBadCertificate: (cert) => true);
//Test if second socket connection accepted or not
response = await _socket.readResponse();
//some server return two lines 125 and 226 for transfer finished
bool isTransferCompleted = response.isSuccessCode();
if (!isTransferCompleted && response.code != 125 && response.code != 150) {
throw FTPConnectException('Connection refused. ', response.message);
}
_socket.logger.log('Start uploading...');
var received = 0;
int fileSize = await fFile.length();
var sink = fFile.openWrite(mode: FileMode.writeOnly);
await secureDataSocket.listen((data) { //<-------- Added -------
sink.add(data);
if (onProgress != null) {
received += data.length;
var percent = ((received / fileSize) * 100).toStringAsFixed(2);
//in case that the file size is 0, then pass directly 100
double percentVal = double.tryParse(percent) ?? 100;
if (percentVal.isInfinite || percentVal.isNaN) percentVal = 100;
onProgress(percentVal, received, fileSize);
}
}).asFuture();
await secureDataSocket.close(); //<-------- Added -------
await sink.flush();
await sink.close();
// Stream<List<int>> readStream = fFile.openRead().transform(
// StreamTransformer.fromHandlers(
// handleData: (data, sink) {
// sink.add(data);
// if (onProgress != null) {
// received += data.length;
// var percent = ((received / fileSize) * 100).toStringAsFixed(2);
// //in case that the file size is 0, then pass directly 100
// double percentVal = double.tryParse(percent) ?? 100;
// if (percentVal.isInfinite || percentVal.isNaN) percentVal = 100;
// onProgress(percentVal, received, fileSize);
// }
// },
// ),
// );
// await dataSocket.write(fFile.readAsBytesSync());
// // await dataSocket.flush();
// await dataSocket.close();
if (!isTransferCompleted) {
// Test if All data are well transferred
response = await _socket.readResponse();
if (!response.isSuccessCode()) {
throw FTPConnectException('Transfer Error.', response.message);
}
}
_socket.logger.log('File Uploaded!');
return true;
}