简体   繁体   English

使用Indy将文件上传到FTP服务器

[英]Upload file to FTP Server using Indy

I am unable to upload a file to my 000webhost.com FTP Server using Indy for Lazarus. 我无法使用Indy for Lazarus将文件上传到我的000webhost.com FTP服务器。 I've tested the ftp connectivity with Windows Command Prompt, and it works fine. 我已经使用Windows命令提示符测试了ftp连接,它运行正常。 These are my Settings (IdFTP): 这些是我的设置(IdFTP):

IdFTP1.Host:='shabala.com';
IdFTP1.Passive:=True;
IdFTP1.TransferType:=ftBinary;
IdFTP1.Username:='******';
IdFTP1.Password:='******';
IdFTP1.Port:=21;

And this is the code which I use to call my TIdFTP component, IdFTP1: 这是我用来调用我的TIdFTP组件IdFTP1的代码:

IdFTP1.Connect(True);
//IdFTP1.ChangeDir('/Sessions');
IdFTP1.Put(GetCurrentDir+'\'+Token+'.cmd',Token+'.cmd', False);
IdFTP1.Quit;
IdFTP1.Disconnect;

where the variable Token is declared as: 其中变量Token声明为:

Token: String; 

When I ran the program for the first time, it kept freezing and I declared a TIdAntiFreeze component to prevent it from freezing. 当我第一次运行程序时,它一直冻结,我宣布了一个TIdAntiFreeze组件,以防止它冻结。 So, this is what happens now: sometimes the program works fine, but no files are transferred to the server (If I try to repeat the upload, it gives me an EIdAlredyConnected error), and sometimes (if I change the code a bit, nothing extra) it gives me an EIdProtocolReplyError with a strange message. 所以,这就是现在发生的事情:有时程序工作正常,但没有文件传输到服务器(如果我尝试重复上传,它会给我一个EIdAlredyConnected错误),有时候(如果我稍微更改了代码,没有什么额外的)它给了我一个带有奇怪消息的EIdProtocolReplyError。 I tried to catch the exception and get my program to display the message, and I've got some strange characters: 我试图捕获异常并让我的程序显示消息,我有一些奇怪的字符:

$ £ ï túÁÕÖ îÖõ)€¶K…ÅõÞl%ÇðåÀ¨Á“§pp $£ïtúÁÕÖîÖ€)€¶K...ÅõÞl%ÇðåÀ¨Á“§pp
A¨%˜ßï7!ƒDªÉ[…oˆ_£P*¡°z1K¢H€Î¨ERPö/ A%~ßï7!ƒDªÉ[... O_£P *¡°z1K¢€^ hΨERPö/
üð΃ç±ïpļƒÏƒ‹Ò1ì üð΃ç±ïpļƒÏƒ<Ò1ì
¿Á{»(g{å¥r…Ž¹öÐR_JúѯuBûŸ€Œ Pp6o¯c[JgžÎ¿Èà¦Ä€VJþz'0è–`BO@T ¿Á{»(g {å¥r ...Ž¹öÐR_JúѯuBûŸ€Pp6o¯c[JgžÎ¿Èà|||VJþz'0è-`BO @ T

The response looks like this if formatted correctly: 如果格式正确,响应将如下所示:

奇怪的回应

I couldn't put the formatted text here directly. 我无法直接在这里放置格式化文本。

The server works absolutely fine, the directories I'm trying to upload to are chmodded to 777, and I've discovered that the file's size (which I want to upload) isn't greater than 3 KBs. 服务器工作得很好,我试图上传到的目录被修改为777,我发现文件的大小(我要上传的)不超过3 KB。

Any ideas? 有任何想法吗?

I have struggled a bit with Indy Ftp over the years. 多年来,我与Indy Ftp有点挣扎。 At some point I turned to an alternative (free) Ftp client from OverbyteIcs (click ICS and then click Download ICS-V8.16 (Apr, 2015)) . 在某些时候,我转向OverbyteIcs的替代(免费)Ftp客户端(单击ICS,然后单击下载ICS-V8.16(2015年4月)) If you are not against using a freeware package, the following code will do the job: 如果您不反对使用免费软件包,以下代码将完成这项工作:

uses
  ...
  OverbyteIcsFtpCli;

procedure FtpUploadFile( 
                             HostName: String; 
                             UserName: String; 
                             Password: String; 
                             UploadFileName: String; 
                             ToHostDir : String );
var
  FTP: TFtpClient;
begin
  FTP := TFtpClient.Create(nil);
  try
    FTP.HostName := HostName;
    FTP.Passive := True;
    FTP.Binary := True;
    FTP.Username := UserName;
    FTP.Password := Password;
    FTP.Port := '21';

    if not FTP.Open then
      raise Exception.Create('Failed to connect: ' + FTP.ErrorMessage);

    if (not FTP.User) or (not FTP.Pass) then 
      raise Exception.Create('Failed to login: ' + FTP.ErrorMessage);

    FTP.HostDirName := ToHostDir;
    if not FTP.Cwd then
      raise Exception.Create('Failed to change dir: ' + FTP.ErrorMessage);

    FTP.LocalFileName := UploadFileName;
    FTP.HostFileName := ExtractFileName(UploadFileName);

    if not FTP.Put then
      raise Exception.Create('Failed to upload file: ' + FTP.ErrorMessage);
  finally
    FTP.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   FtpUploadFile('rubilaxe.hostoi.com',  
                     '******', '******',
                     IncludeTrailingPathDelimiter( 
                          ExtractFilePath(Application.ExeName) ) +'datafile.zip',
                     '/files'  );
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM