Recent

Author Topic: (SOLVED) FTP download in memory  (Read 5447 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
(SOLVED) FTP download in memory
« on: June 06, 2016, 11:39:38 am »
Hello guys, does anyone have an example of an FTP download with synapse but that does not create the file but rather charge me the downloaded image directly into a TPicture? In short working only in RAM to be faster. Thank you
« Last Edit: June 07, 2016, 01:04:31 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

balazsszekely

  • Guest
Re: FTP download in memory
« Reply #1 on: June 06, 2016, 12:11:45 pm »
With Indy you can. There is an overloaded version of IdFTP.Get method which accepts as parameter a stream:
Code: Pascal  [Select][+][-]
  1. procedure Get(const ASourceFile: string; ADest: TStream; AResume: Boolean = false); overload;
Looking at the synapse code, I see the FtpGetFile function, which internally use the RetriveFile then the DataRead method. DataRead accepts a stream parameter, so in theory it's possible but you have to do a little work.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: FTP download in memory
« Reply #2 on: June 06, 2016, 12:15:15 pm »
Just set DirectFile to false. It will use the FDataStream (TMemoryStream) to read the file in memory.

You'll have to do something like this:

Code: Pascal  [Select][+][-]
  1. uses ftpsend;
  2.  
  3. function ReadFtpToImage(const IP, Port, FileName, User, Pass: string; Image1: TImage): boolean;
  4. begin
  5.   Result := False;
  6.   with TFTPSend.Create do
  7.     try
  8.       if User <> '' then
  9.       begin
  10.         Username := User;
  11.         Password := Pass;
  12.       end;
  13.       TargetHost := IP;
  14.       TargetPort := Port;
  15.       if not Login then Exit;
  16.       DirectFileName := 'dummyfilename';
  17.       DirectFile := False;     // <------ Important !!
  18.       Result := RetrieveFile(FileName, False);
  19.  
  20.       if Result then
  21.         Image1.Picture.LoadFromStream(DataStream);
  22.  
  23.       Logout;
  24.     finally
  25.       Free;
  26.     end;
  27. end;

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: FTP download in memory
« Reply #3 on: June 07, 2016, 08:39:21 am »
Ok rvk, it works great , and if I wanted to do it reverse ? That is, send to FTP the contents of a TImage component ?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: FTP download in memory
« Reply #4 on: June 07, 2016, 10:12:01 am »
I've taken the source of FtpGetFile() to create the ReadFtpToImage.
So for the reverse you could have taken the FtpPutFile() and done the same.

Here is a (non-tested) example:
Code: Pascal  [Select][+][-]
  1. uses ftpsend;
  2.  
  3. // source taken from FtpPutFile()
  4. function WriteImageToFtp(const IP, Port, FileName, User, Pass: string; Image1: TImage): boolean;
  5. begin
  6.   Result := False;
  7.   with TFTPSend.Create do
  8.     try
  9.       if User <> '' then
  10.       begin
  11.         Username := User;
  12.         Password := Pass;
  13.       end;
  14.       TargetHost := IP;
  15.       TargetPort := Port;
  16.       if not Login then Exit;
  17.       DirectFileName := 'dummyfilename';
  18.       DirectFile := False;        // <------ Important !!
  19.  
  20.       Image1.Picture.SaveToStream(DataStream); // <-- this saves the image to ftp-stream
  21.  
  22.       Result := StoreFile(FileName, False); // and transfers it to Filename on ftp server
  23.       Logout;
  24.     finally
  25.       Free;
  26.     end;
  27. end;

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: FTP download in memory
« Reply #5 on: June 07, 2016, 11:20:41 am »
But with the ' last example I refer to the file What ftp server resides on HD , I want to pass the content of image , then RAM to FTP
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: FTP download in memory
« Reply #6 on: June 07, 2016, 11:23:39 am »
Yes, that's what the WriteImageToFtp() I showed you does. It does the reverse from the previous ReadFtpToImage().

You supply WriteImageToFtp with a filename to store on the FTP side and the TImage with content. It will take the content of that TImage and store it on the FTP-side in Filename.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: FTP download in memory
« Reply #7 on: June 07, 2016, 01:04:17 pm »
You're right , it works well . thank you so much
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018