tags:

views:

577

answers:

3

I'm using the IdFTP (Indy 10) component to download some files (zip and txt) from a remote location. Before getting each file I set the TransferType to binary.

IdFTP.TransferType := ftBinary;
IdFTP.Get(ASource, ADest, AOverwrite);

I expect that both text and binary files can be downloaded using the binary mode. However it looks like text files contents is messed up while zip files are downloaded correctly. If I set the TransferType to ASCII for text files it works as expected. How can I detect which TransferType to set for a given file? Is there a common denominator or auto setting?

+6  A: 

I don't see how the Binary flag can mess up transferred files. Binary type means the server transfers the files without any processing, as is.

The only thing that an FTP server should use the ASCII flag for, is to correctly handle the end of line in text files, usually (1) either only Line Feed in Unix or (2)Carriage Return + Line Feed in Windows. But nowadays most text editors handle both in either system.

So the safest is use only ASCII flag for very well known text files, probably only files with a .txt extension, and use Binary flag for all the others.

PA
+1. Yep - many FTP programs base this off the file extension when in Auto mode.
TrueWill
A: 

I guess it would also depend on how you are viewing the text file, ANSI or WideChar as to whether the text is messed up or not.

+3  A: 

When in doubt, rule it out (!) - try transferring the files from the server using the Windows commandline FTP program, and see if text files still come out wrong. The program will transfer binary (command BIN) or text (command ASCII). If you transfer files with this and they still arrive differently to your expectation, then something is being done at the server end*. If they arrive fine, then either you (or Indy) are doing something. :-)

*In what way are the text files messed up? If you're transferring unicode text files, you might be better off transferring them as BINary anyway. I must admit that, as @unknown (yahoo) said, in most cases you should probably stick to BIN mode.

robsoft
Thanks for the hint! I followed your advice and using the command line ftp program I get a similar issue: when I set the mode to binary the text files are corrupted but if the mode is ascii everything is fine. I also used FileZilla and I wonder how it manages to set the right transfer mode for each file.
Tihauan
Cool. How are they corrupted? I wonder if you're fetching files from a Unix/Linux/Mac server and your corruption is just the missing 'CRLF' pairings (It's either just CR or LF, I forget which, on the non-Windows systems). In terms of the program guessing the filetype, I think you could get a reasonable result from looking at the file extension and if that doesn't help, look at the first (say) 200 bytes of the file (in ascii mode). If there's no CRLF pairing in that chunk, switch to binary transfer again, otherwise just carry on with the ascii transfer...? Just a suggestion, not experience!
robsoft
First 64 bytes in binary mode "Ô–¥…”…•£ÉÄ_Ô–¥…”…•£Ä…¢ƒ™‰—£‰–•_ã–¤™ÉÄ_ã–¤™â…˜¤…•ƒ…_â…ƒ£–™ÉÄ_⃈" and in text mode "MovementID¬MovementDescription¬TourID¬TourSequence¬SectorID¬Sch".
Tihauan
robsoft