TheGreenOmega
TheGreenOmega

Reputation: 133

Upload file to FTP Server using Indy

I am unable to upload a file to my 000webhost.com FTP Server using Indy for Lazarus. I've tested the ftp connectivity with Windows Command Prompt, and it works fine. These are my Settings (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:

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: String; 

When I ran the program for the first time, it kept freezing and I declared a TIdAntiFreeze component to prevent it from freezing. 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. 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
A¨%˜ßï7!ƒDªÉ[…oˆ_£P*¡°z1K¢H€Î¨ERPö/
üð΃ç±ïpļƒÏƒ‹Ò1ì
¿Á{»(g{å¥r…Ž¹öЭR_JúѯuBûŸ€Œ Pp6o¯c[JgžÎ¿­Èà¦Ä€VJþz’0è–`BO@T

The response looks like this if formatted correctly:

Strange response

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.

Any ideas?

Upvotes: 3

Views: 19162

Answers (1)

I have struggled a bit with Indy Ftp over the years. At some point I turned to an alternative (free) Ftp client from OverbyteIcs (click ICS and then click Download ICS-V8.16 (Apr, 2015)). 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;

Upvotes: 9

Related Questions