Omair Iqbal
Omair Iqbal

Reputation: 1838

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.

Upvotes: 0

Views: 3796

Answers (3)

robsoft
robsoft

Reputation: 5585

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.

Upvotes: 2

Francesca
Francesca

Reputation: 21640

And why don't you try a little harder to see what's going on by testing all the return codes to be sure of where it fails?

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);
  if hInet = nil then
    RaiseLastOSError;

  hConnect := InternetConnect(hInet,
    remote_server,
    INTERNET_DEFAULT_FTP_PORT,
    user, pass,
    INTERNET_SERVICE_FTP,
    INTERNET_FLAG_PASSIVE,
    0);
  if hConnect = nil then
    RaiseLastOSError;

  if not ftpPutFile(hConnect, local_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) then
    RaiseLastOSError;

  if not InternetCloseHandle(hConnect) then
    RaiseLastOSError;
  if not InternetCloseHandle(hInet) then
    RaiseLastOSError;
end;

You don't even know if you got the connection before trying to send the file... (It's what I got, as expected, when running the code with these site/user/password values)

And if you get past that, you may actually get a detailed explanation as to why ftpPutFile fails, like in this example:

System Error.  Code: 3.

The system cannot find the path specified.

Upvotes: 4

pingw33n
pingw33n

Reputation: 12510

Check return value of FtpPutFile (it should return TRUE on success) and get a detailed error with GetLastError.

Upvotes: 4

Related Questions