'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:
and immediately after another:
and the button disappears.
Why? Thanks!
Solution 1:[1]
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;
Solution 2:[2]
You must create the instance of the tidftp
first.
var
FTP:tidftp;
begin
FTP:=Tidftp.Create(nil); //create the instance
try
FTP.Host:='siteweb.altervista.org';
FTP.Username:='siteweb';
FTP.Password:='password';
FTP.Port:=25;
FTP.Connect;
FTP.Put('C:\Users\user\Desktop\text.txt');
FTP.Quit;
finally
FTP.Free;
end;
end;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | RRUZ |