Search code examples
delphiapiftpwininet

why is my program not uploading file on remote ftp server?


i coded this program to upload a file on server using ftpput api it is not working it runs but file is not delevered!

here's the code:

unit ftp3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,wininet;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);

       var hInet, hConnect: HINTERNET; 

    local_file, 
    remote_file,
    user,remote_server,
    pass: pchar;

    begin 
    local_file := 'C:\Documents and Settings\Omair\Desktop\loggen.txt';
    remote_file := 'loggen.txt';
    user := 'my user';
    pass := 'my pass';
    remote_server := ' ftp.drivehq.com';

    hInet := InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
    hConnect := InternetConnect(hInet,
        remote_server, 
        INTERNET_DEFAULT_FTP_PORT,
        user, pass,
        INTERNET_SERVICE_FTP,
        INTERNET_FLAG_PASSIVE,
        0);

    ftpPutFile(hConnect, local_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);

    InternetCloseHandle(hInet);
    InternetCloseHandle(hConnect);

    end;   

  end.

Solution

  • First of all, rule-out any major errors in your program (or rule them in, if you like) by checking you can FTP that same file with Microsoft's built-in FTP program.

    From the command line, type

    FTP ftp.drivehq.com (return)

    if this doesn't come back to you with a login prompt, you have problems outside of your Delphi code. Either you have limited internet connectivity (maybe port 25, the FTP port, is blocked by your firewall/router) or there is a problem with the FTP address itself.

    If you do get a prompt, enter your username and password when asked. Now type

    BIN (return)
    PUT 'C:\Documents and Settings\Omair\Desktop\loggen.txt' (return)

    if that seems to send your file, (type BYE to leave the FTP program, by the way) then your problem is with your Delphi code rather than with the FTP process itself (the other answers here have helpfully pointed out things you need to check in the Delphi code itself). If it doesn't seem to send the file, again I suggest you look to resolving that problem before you carve-up your Delphi code.

     

    When I'm doing any kind of 'online' work like this, I always try to get a separate process for testing the 'other end' of the system, one that doesn't use any of my own code.