2

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).

enter image description here

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...

0

1 Answer 1

1

Those empty spaces in Beyond Compare are not space characters (I do not see any 20). Those are missing bytes [when compared to the left-hand side]. The differences are 0A (LF) [right] being converted to 0D+0A (CRLF) [left] – a sign of a text mode transfer.

I do not know Flutter nor Dart, but it seems, that you need to call FTPConnect.setTransferType with TransferType.binary:

_ftpConnect.setTransferType(TransferType.binary);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.