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:
Specify the FTP server's address
Specify the username to login
Specify the password for the username
Connect to the FTP server
Start receiving a file to memory
Process the data being received
Disconnect from the FTP server
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.
{ 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);