Jump to content
Guest

FTP download file

Recommended Posts

Guest

Hello,
I have delphi 7 with ICS installed and can upload file with that code:

Function FtpUploadFile(Host: String;UserName: String;Password: String;UploadFileName: String;ToHostDir,Port : String ):Boolean;
var
FTP:TFtpClient;
begin

Result:=False;

  try
    Screen.Cursor:=crHourGlass;
    FTP:=TFtpClient.Create(nil);
    FTP.HostName := Host;
    FTP.Passive := True;
    FTP.Binary := True;
    FTP.Username := UserName;
    FTP.Password := Password;
    FTP.Port := Port;

    if not FTP.Open then
    begin
    //  If Assigned(FTP) then FTP.Free;
      MessageBeep(MB_ICONEXCLAMATION);
      MessageDlg('Echecs de connexion: '+#13+ FTP.ErrorMessage,mtError,[mbOk],0);
      Abort;
    end;

    if (not FTP.User) or (not FTP.Pass) then
    begin
 ///     If Assigned(FTP) then FTP.Free;
      MessageBeep(MB_ICONEXCLAMATION);
      MessageDlg('Echecs d''authentification: '+#13+ FTP.ErrorMessage,mtError,[mbOk],0);
      Abort;
    end;

    FTP.HostDirName := ToHostDir;
    if not FTP.Cwd then
    begin
  //    If Assigned(FTP) then FTP.Free;
      MessageBeep(MB_ICONEXCLAMATION);
      MessageDlg('Echecs changement dossier: '+#13+ FTP.ErrorMessage,mtError,[mbOk],0);
      Abort;
    end;

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

    if not FTP.Put then
    begin
   //   If Assigned(FTP) then FTP.Free;
      MessageBeep(MB_ICONEXCLAMATION);
      MessageDlg('Echecs à l''envoi du fichier: '+#13+ FTP.ErrorMessage,mtError,[mbOk],0);
      Abort;
    end;
  finally
   // If Assigned(FTP) then FTP.Free;
    Result:=True;
    Screen.Cursor:=crDefault;
  end;
 end;

 

I need to know please how can I modify this code for downloading file

Regards,

 

Edited by Guest

Share this post


Link to post

Never used ICS's TFTPClient, but have you tried ftp.Get instead ftp.Put?

 

You are leaking "FTP'. Uncomment the line in the finally section.

Also, create the object outside, before the Try..Finally block.

Share this post


Link to post

I can't answer your question for ICS. I use Curl because it supports FTP/FTPS/SFTP. You can find all the information on how to use it in this forum thread.

 

An example of a download function:

function FTPDownloadFile(const pmcUrl, pmcUserName, pmcPassword: RawUtf8; var pmvContent: RawByteString): TCurlResult;
var
  hnd: TCurl;
begin
  Result := crFailedInit;
  if not CurlIsAvailable then Exit; //=>

  hnd := curl.easy_init;
  if hnd <> Nil then
  begin
    curl.easy_setopt(hnd, coURL, Pointer(pmcUrl));
    curl.easy_setopt(hnd, coUserName, Pointer(pmcUserName));
    curl.easy_setopt(hnd, coPassword, Pointer(pmcPassword));
    curl.easy_setopt(hnd, coWriteFunction, @CurlWriteRawByteString);
    curl.easy_setopt(hnd, coWriteData, @pmvContent);
    Result := curl.easy_perform(hnd);
    if Result <> crOk then
      pmvContent := '';

    curl.easy_cleanup(hnd);
  end;
end;

Use it like this (for an SFTP server):

uses
  mormot.core.base,
  mormot.core.text,
  mormot.core.os,
  mormot.lib.curl;

var
  url: RawUtf8;
  buffer: RawByteString;
begin
  url := 'sftp://test.rebex.net:22/pub/example/readme.txt';
  if FTPDownloadFile(url, 'demo', 'password', buffer) = crOk then
  begin
    FileFromString(buffer, MakePath([Executable.ProgramFilePath, 'readme.txt']));
    ShowMessage(Format('Download completed: %s', [KB(Length(buffer))]));
  end;
end;

With best regards

Thomas

Share this post


Link to post

You should be using the TIcsFtpMulti component then you can use the FtpUpOneFile and FtpDownOneFile methods (or process hundreds of files in FtpDownFiles).

 

Look at the OverbyteIcsXferTst.dpr sample. You only need a few lines of code in the application.

 

Angus

 

Share this post


Link to post
Guest

I will try all solutions and see samples thanks you very much for all

Share this post


Link to post
Guest
On 2/9/2023 at 2:02 PM, aehimself said:

You are leaking "FTP'. Uncomment the line in the finally section.

Also, create the object outside, before the Try..Finally block.

I Uncomment the line in the finally section and created outside the object  because before I had access violation

Thanks aehimself

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×