Search code examples
windowsdelphiftpindy

Delphi FTP upload Access violation


I'm trying to upload a file .txt in my web space, but then the problems start, the code I tried is this:

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase, IdFTP, StdCtrls;

procedure TForm1.Button1Click(Sender: TObject);
var
FTP:tidftp;
begin
FTP.Host:='website.altervista.org';
FTP.Username:='website';
FTP.Password:='password';
FTP.Port:=25;
FTP.Connect;
FTP.Put('C:\Users\user\Desktop\text.txt');
FTP.Quit;
end;

I'm not getting any error, but when I start the program and I click on the button, I get an error:

enter image description here

and immediately after another:

enter image description here

and the button disappears.

Why? Thanks!


Solution

  • You have to instantiate the TIdFTP object for your local variable FTP before you access it. So try to use this:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      FTP: TIdFTP;
    begin
      FTP := TIdFTP.Create(nil);
      try
        FTP.Host := 'serioussamhd.altervista.org';
        FTP.Username := 'serioussamhd';
        FTP.Password := 'password';
        FTP.Port := 21;
        FTP.Connect;
        FTP.Put('C:\Users\user\Desktop\text.txt');
        FTP.Quit;
      finally
        FTP.Free;
      end;
    end;