Search code examples
delphiftpindyindy10

INDY10 Stream write error while I trying to send data trought FTP


I want to put data from Memo1 directly to my FTP server, I've got code:

procedure TForm5.SendClick(Sender: TObject);  
var K: TStream;  
begin  
    K := TStream.Create;
    Memo1.Lines.SaveToStream(K);
    FTP.Host := 'localhost';
    FTP.Username := 'login';
    FTP.Password := 'haslo';
    FTP.Connect;
    if FTP.Connected then FTP.Put(K,'');
end;

But when I click "Send" button I've got two errors:

enter image description here

when Memo is empty

enter image description here

when I try send data


Solution

  • TStream is an abstract class. You must never instantiate it. Use a concrete class instead like, for instance, TMemoryStream.

    You'll also want to destroy the stream when you are finished with it, or it will leak. Do yourself a favour and set ReportMemoryLeaksOnShutdown to True, for instance in your .dpr file. That will allow you to get a report of all the memory you are leaking when your program terminates.

    Your code might run like this:

    var 
      Stream: TMemoryStream;  
    ....
    Stream := TMemoryStream.Create;
    try
      // .... initialize the Indy object
      if FTP.Connected then begin
        // .... populate stream
        Stream.Position := 0;
        FTP.Put(Stream, '');
      end;
    finally
      Stream.Free;    
    end;