Xceed FTP Library Documentation
Example topics / Receiving a file to memory (Delphi example)
In This Topic
    Receiving a file to memory (Delphi example)
    In This Topic

    This example demonstrates the 8 following steps for receiving a file's data directly to memory: 

    1. Specify the FTP server's address 
    2. Specify the username to login 
    3. Specify the password for the username 
    4. Connect to the FTP server 
    5. Start receiving a file to memory 
    6. Process the data being received 
    7. Disconnect from the FTP server 
    8. Perform error handling  

    The following code assumes you have placed an XceedFtp control and a listbox on a form, named 'XceedFtp1' and 'Listbox1' respectively. 

    The first section of code below demonstrates all steps except step (6). Step (6), the handler for the ReceivingMemoryFileData event is found right after. 

    Delphi Copy Code

    begin
       XceedFtp1.License( 'your license key' );

       XceedFtp1.ServerAddress := '192.168.0.200'; {step (1)}
       XceedFtp1.UserName := 'anonymous'; {step (2)}
       XceedFtp1.Password := 'guest'; {step (3)}

       try {step (8)}
          XceedFtp1.Connect(); {step (4)}
          ShowMessage('Successfully connected to server. Starting the receive operation.');

          try {step (8)}
             XceedFtp1.ReceiveMemoryFile('readme.txt', 0, fmoSingleBlock); {step (5)}

            { In the line above, we chose to receive the data in a single
              block of data. So the ReceivingMemoryFile event will only
              be triggered once. You can change the value of the third
              parameter to receive the data in many smaller chunks,
              whenever data is received by the Xceed FTP Library. }

             ShowMessage('Successfully received the file to memory');
             except
                on xErr: Exception do
                   Listbox1.Items.Add('Receive error: ' + xErr.Message);
             end;

             try {8}
                XceedFtp1.Disconnect(); {7}
             except
                on xErr: Exception do
                    Listbox1.Items.Add('Disconnect error: ' + xErr.Message);
             end;
          except
             on xErr: Exception do
                Listbox1.Items.Add('Connect error: ' + xErr.Message);
          end;

    { Step (6): The following code, which simply displays the received
    data in the listbox on the form, must go into the error
    handler for the ReceivingMemoryFileData event. }

    procedure TForm1.XceedFtp1ReceivingMemoryFileData(Sender: TObject; const sRemoteFilename: WideString; lFileSize: Integer; var vaData: OleVariant; bEndOfData: WordBool);
    var
       pcData : PChar;
       nMaxLength : Integer;
       pcCopyData : PChar;
    begin
       // Perform some conversion so we can display data in listbox...
       nMaxLength := VarArrayHighBound(vaData, 1) - VarArrayLowBound(vaData,1) + 1;
       pcData := VarArrayLock(vaData);
       GetMem(pcCopyData, (nMaxLength + 1));
       StrLCopy(pcCopyData, pcData, nMaxLength);
       pcCopyData[nMaxLength] := #0;
       VarArrayUnlock(vaData);

       Listbox1.Items.Add(pcCopyData);
    end;