The problem I'm dealing with is related to handling binary files, specifically wav files. The sample code for text files is working like a charm – I can connect to FTP, download, and upload files without any issues. However, when it comes to downloading wav files, the resulted audio sounds distorted.
Upon further investigation, I discovered that the spaces (0x20) in the original file on the FTP server (named message2.wav) were being converted to 0x0D in the downloaded version (let's call it DownloadedStepByStep.wav).
I have shared the files for reference here . Thanks for your understanding!
Here is the full source code which is slightly changed (the FTP credentials removed):
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:ftpconnect/ftpconnect.dart';
import 'dart:convert' show Encoding, LineSplitter, Utf8Codec, Utf8Decoder, Utf8Encoder, utf8;
void main() async {
final FTPConnect _ftpConnect = new FTPConnect( "ftp.xxxxxx.com", user: "[email protected]", pass: "xxxxx", showLog: true , port: 21 );
///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
const X=<int>[]; // added by me as input of List<int> to function below
Future<File> _fileMockwav({fileName = 'FlutterTest.wav' , content =X }) async {
final Directory directory = Directory('/test')..createSync(recursive: true);
final File file = File('${directory.path}/$fileName');
await file.writeAsBytes(content ); // changed by me
return file;
}
Future<void> _downloadStepByStep() async {
try {
await _log('Connecting to FTP ...');
await _ftpConnect.connect();
await _log('Downloading ...');
String fileName = '../message2.wav';
//here we just prepare a file as a path for the downloaded file
File downloadedFile = await _fileMockwav(fileName: 'DownloadedStepByStep.wav');
await _ftpConnect.downloadFile(fileName, downloadedFile);
await _log('file downloaded path: ${downloadedFile.path}');
await _ftpConnect.disconnect();
} catch (e) {
await _log('Downloading FAILED: ${e.toString()}');
}
}
await _downloadStepByStep();
}
The wav file should not be distorted and the conversion of spaces to 0x0d is the problem. No idea on how to avoid this conversion...