Indy FTP未能无法上传 [英] Indy FTP Failing to upload miserably
问题描述
使用一个简单的代码,例如:
procedure TForm1.cxButton1Click(Sender:TObject);
begin
ftp.Host:='domain';
ftp.Username:='user';
ftp.Password:='密码';
ftp.Connect;
ftp.Put('C:\_Projects\testpicture.JPG');
ftp.Quit;
ftp.Disconnect;
end;
我得到以下结果:
- 上传时应用程序会冻结(如果无法查看进度条位置)。
- 上传的文件损坏(破坏的内容超过几个字节)。 li>
我究竟做错了什么?
谢谢。
由于Indy使用阻止操作,因此应用程序冻结。在代码运行时,主消息循环没有运行,因此在 cxButton1Click()
退出之前不会处理新消息。为了解决这个问题,可以在 TForm
上放置一个 TIdAntiFreeze
组件,或者移动 TIdFTP
代码添加到单独的工作线程,然后使用 TIdSync
或 TIdNotify
来更新UI安全的。
如果您使用ASCII模式而不是二进制模式传输文件,该文件将被损坏。确保 TIdFTP.TransferType
属性设置为 ftBinary
。 Indy 9和更早的版本默认为 ftBinary
,但Indy 10默认为 ftASCII
代替(符合FTP协议规范)。
Using a simple code, such as:
procedure TForm1.cxButton1Click(Sender: TObject);
begin
ftp.Host := 'domain';
ftp.Username := 'user';
ftp.Password := 'password';
ftp.Connect;
ftp.Put('C:\_Projects\testpicture.JPG');
ftp.Quit;
ftp.Disconnect;
end;
I'm getting the following results:
- Application freezes while uploading (ergo unable to see Progress Bar position).
- Uploaded file goes corrupted (corrupts anything more than a few bytes).
What on earth am I doing wrong?
Thank you.
The app freezes because Indy uses blocking operations. While the code is running, the main message loop is not running, so new messages are not being processed until cxButton1Click()
exits. To solve that, either place a TIdAntiFreeze
component onto your TForm
, or else move the TIdFTP
code to a separate worker thread, and then use TIdSync
or TIdNotify
to update the UI safely when needed.
The file will be "corrupted" if you are transferring it in ASCII mode instead of in binary mode. Make sure the TIdFTP.TransferType
property is set to ftBinary
. Indy 9 and earlier defaulted to ftBinary
, but Indy 10 defaults to ftASCII
instead (to match the FTP protocol specs).
这篇关于Indy FTP未能无法上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!