Recent

Author Topic: Cannot get FTP to work  (Read 544 times)

garymq

  • New Member
  • *
  • Posts: 12
Cannot get FTP to work
« on: May 19, 2026, 02:54:07 am »
Hello

I have been trying to use a lazarus procedure to upload a file, using the Synapse (40.1) FTP package.
However the upload fails. I'd like some help in figuring out why.

I am using the FTP site ftp.dlptest.com to test it. (publicly available user/pw are in the code)
This is a public website anyone can use to test FTP uploads.
The relevant procedure is below.

Note 1: I have manually used ftp on the command line, manually logged into ftp.dlptest.com (using the same user/pw), and manually uploaded the test file. Everything works.

Note 2: The code correctly finds the local file, and logs into ftp.dlptest.com correctly.
But the upload just does not happen, and returns an error message which is simply 0, as shown in the log.

Can anyone say what I am doing wrong, or suggest things to try, or even suggest alternative code?

Thanks, Gary

Code: Pascal  [Select][+][-]
  1. procedure TFrm_AquaView.FTPfile();
  2. var
  3.  Ftp: TFTPSend;
  4. begin
  5.   Ftp := TFTPSend.Create;
  6.   try
  7.     Log('starting log in');
  8.     Ftp.TargetHost := 'ftp.dlptest.com';
  9.     Ftp.TargetPort := '21';
  10.     Ftp.Username := 'dlpuser';
  11.     Ftp.Password := 'rNrKYTX9g7z3RgJRmxWuGHbeu';
  12.     //Ftp.AutoTLS := True; // For FTPS/Secure connection [10]
  13.  
  14.     if Ftp.Login then
  15.     begin
  16.       Log('Logged in successfully');
  17.       Ftp.DirectFile := True;
  18.  
  19.       If FileExists('testfile.txt') then Log('found testfile.txt');
  20.  
  21.     if Ftp.StoreFile('testfile.txt', False) then
  22.      Log('Upload complete: ' + 'testfile.txt')
  23.     else
  24.      Log('Upload failed. Last error: ' + IntToStr(Ftp.DSock.LastError));
  25.     end;
  26.      Ftp.Logout;
  27.   except
  28.    on E1: Exception do
  29.    ShowMessage('Error during ftp:' +E1.Message);
  30.   end;
  31.   Ftp.Free;
  32.  
  33. end;
  34.  

Here is the log output:
Code: Pascal  [Select][+][-]
  1. MemoLog
  2. 10:31:50 - starting log in
  3. 10:31:52 - Logged in successfully
  4. 10:31:52 - found testfile.txt
  5. 10:31:52 - Upload failed. Last error: 0
  6.  



Hartmut

  • Hero Member
  • *****
  • Posts: 1151
Re: Cannot get FTP to work
« Reply #1 on: May 19, 2026, 06:05:31 am »
It's long ago that I wrote my FTP-Upload with Synapse and I forgot the most details, but from looking in my old code I assume that you forgot a
Code: Pascal  [Select][+][-]
  1. Ftp.DirectFileName:='testfile.txt';
after line 17 = "Ftp.DirectFile := True;"

As far as I remember: with 'DirectFileName' you tell the local file name and with StoreFile() the remote filename.

garymq

  • New Member
  • *
  • Posts: 12
Re: Cannot get FTP to work
« Reply #2 on: May 19, 2026, 06:26:51 am »
hi Hartmut

That's great!
It works now.

The log now reads
Code: Pascal  [Select][+][-]
  1. 14:20:26 - starting log in
  2. 14:20:28 - Logged in successfully
  3. 14:20:28 - found testfile.txt
  4. 14:20:30 - Upload complete: testfile.txt
  5.  
  6.  

I am a bit surprised we need to specify the file twice: Once in
Ftp.DirectFileName:='testfile.txt'
and again in
Ftp.StoreFile('testfile.txt', False)

Anyway, that's just what I wanted.

Thanks very much!
Gary


Thaddy

  • Hero Member
  • *****
  • Posts: 19278
  • Glad to be alive.
Re: Cannot get FTP to work
« Reply #3 on: May 19, 2026, 07:15:33 am »
The name is not really twice: their file name and your file name. You can store under a different name.
objects are fine constructs. You can even initialize them with constructors.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1596
    • Lebeau Software
Re: Cannot get FTP to work
« Reply #4 on: May 20, 2026, 01:39:32 am »
The name is not really twice: their file name and your file name. You can store under a different name.

While that is true, I would have expected that if a source filename is not specified then it would use the remote filename as the source filename, and vice versa.
« Last Edit: May 22, 2026, 02:56:12 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 19278
  • Glad to be alive.
Re: Cannot get FTP to work
« Reply #5 on: May 21, 2026, 11:58:52 am »
Agreed.
objects are fine constructs. You can even initialize them with constructors.

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: Cannot get FTP to work
« Reply #6 on: May 21, 2026, 03:32:36 pm »
How would you use Ftp.StoreFile() then to read test.txt but store it as testfile.txt on the FTP server? (so other local filename than remote name)

The first parameter of Ftp.StoreFile is only for the name on the server (not the filename to read).
You can also use a TStream for reading the local file and passing it to Ftp.StoreFile (with DirectFile = false).

I agree that Ftp.StoreFile could have some other overloaded functions in such that DirectFile and DirectFileName are not needed but every developer has some quirks during programming that someone else always says, after coding, O I would have done this differently  :D

You could always create your own StoreFile... there is also already a FtpPutFile() for which you can specify Filename and LocalFile (but that also includes login etc).

Thaddy

  • Hero Member
  • *****
  • Posts: 19278
  • Glad to be alive.
Re: Cannot get FTP to work
« Reply #7 on: May 22, 2026, 02:05:47 pm »
The store file is the client side...?
The source file is the server side.
objects are fine constructs. You can even initialize them with constructors.

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: Cannot get FTP to work
« Reply #8 on: May 22, 2026, 02:08:22 pm »
The store file is the client side...?
No, the filename you supply is the name for storing on the server. It has nothing to do with the local name. You could also store a stream if you don't set directfile.

Thaddy

  • Hero Member
  • *****
  • Posts: 19278
  • Glad to be alive.
Re: Cannot get FTP to work
« Reply #9 on: May 23, 2026, 10:11:01 am »
Sure? Well, ok. That is a bit counter intuitive, though.
objects are fine constructs. You can even initialize them with constructors.

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: Cannot get FTP to work
« Reply #10 on: May 23, 2026, 10:23:48 am »
Sure? Well, ok. That is a bit counter intuitive, though.
I agree. But that happens when someone doesn't create a design framework beforehand  ::)

The class was probably first created to just deal with TStreams. Then, as an afterthought, someone thought, hey it would be easy to have a local file which you can use to upload. And instead of creating a complete new function, the awkward method of introducing DirectFile and DirectFilename was introduced. Not as parameters, but as completely seperate properties  %)

It would have been better to make it an overloaded function (or seperate function) where you can pass both local filename and filename on the server.

You could create a class helper for that though. A complete overhaul would probably break too many applications using it.

 

TinyPortal © 2005-2018