为什么文件传输后有时会有不同的大小或损坏? [英] Why are files sometimes a different size or corrupt after transferring by FTP?

查看:209
本文介绍了为什么文件传输后有时会有不同的大小或损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi XE2中使用TidFTP组件下载文件时遇到问题。我可以获得与FTP站点的连接,获取文件列表并执行get命令。但是,当我尝试使用get命令下载文件时,文件总是下载大于源文件。然后下一个文件被破坏。

I'm having a problem downloading a file using the TidFTP component in Delphi XE2. I am able to get a connection to the FTP site, get a list of files and perform the get command. However, when I try to download a file using the get command the file always downloads larger than the source file. Then the subsequent file is corrupt.

此外,如果我尝试下载多个文件,第一个文件下载(大于源),剩下的文件将被跳过。从get命令抛出错误,它只是退出。我试图钩住TidFTP控件上的一些事件,如AfterGet和OnStatus,但一切都正常。

Additionally, if I try to download multiple files, the first file downloads (larger than the source) and the remaining files are skipped. No error is thrown from the get command, it just quits. I tried to hook into some of the events on the TidFTP control like AfterGet and OnStatus but everything appears normal.

我尝试使用第三方FTP客户端访问该文件,下载它只是为了确保它不是我们的FTP服务器的问题,该下载工作正常。所以我认为它可能与TidFTP控件有关,或者我正在做错误的事情。

I tried using a 3rd party FTP client to access the file and download it just to make sure it was not a problem with our FTP server and that download worked as expected. So I'm thinking it might be related to the TidFTP control or perhaps I'm doing something incorrectly.

这是我用来下载文件的例程:

Here is the routine I'm using to download the file:

var
  ftp: TIdFTP;
  strDirectory: string;
begin
  ftp := TIdFTP.Create(nil);
  try
    ftp.Host := 'ftp.myftpserver.com'
    ftp.Passive := false;
    ftp.Username := 'TestUser';
    ftp.Password := 'TestPassword';
    ftp.ConnectTimeout := 1000;
    ftp.Connect();
    ftp.BeginWork(wmRead);
    ftp.ChangeDir('/TestArea/');
    strDirectory := 'c:\test\';
    if not DirectoryExists(strDirectory) then
      CreateDir(strDirectory);
    ftp.Get('Test.zip', strDirectory + '\' + 'Test.zip', true, false);
   ftp.Disconnect();
  except
    on e: exception do
      showMessage(e.message);
  end;
end;


推荐答案

您需要设置 TIdFTP.TransferType 。其默认值为 Id_TIdFTP_TransferType ,即 ftASCII 。您需要使用 ftBinary ,然后在执行 Get 之前进行设置:

You need to set the TIdFTP.TransferType. Its default value is Id_TIdFTP_TransferType, which is ftASCII. You need to use ftBinary instead, and set it before executing the Get:

ftp.Connect();
...
ftp.TransferType := ftBinary;
ftp.Get('Test.zip', strDirectory + '\' + 'Test.zip', true, false);
ftp.Disconnect();

TIdFTP.TransferType 在某个地方说,当登录被执行或连接时,它会自动设置为 ftBinary AutoLogin 设置为true时调用,但您未执行登录您的代码并没有设置 AutoLogin 。紧跟在上述声明之后的段落说:

The documentation for TIdFTP.TransferType says in one place that it's automatically set to ftBinary when Login is executed or when Connect is called when AutoLogin is set to true, but you're not executing Login in your code and haven't set AutoLogin. The paragraph immediately following the above statement says:

根据@RemyLebeau在下面的评论中,引用的文档是错误的,登录中,没有将TransferType 设置为 ftBinary 。留下受伤的内容供将来参考。

According to @RemyLebeau in the comments below, the documentation quoted is in error, and TransferType was never set to ftBinary in Login. Leaving the stricken content for future reference.

根据文档:


TransferType的默认值为Id_TIdFTP_TransferType,在组件初始化期间分配。

The default value for TransferType is Id_TIdFTP_TransferType, as assigned during initialization of the component.

这篇关于为什么文件传输后有时会有不同的大小或损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆