Xceed .NET Libraries Documentation
How to send files

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Task-Based Help > FTP capabilities > Working with the FtpClient interface > How to send files

Introduction

The SendFile, SendMultipleFiles and SendFileToUniqueName methods of the FtpClient class as well as the BeginSendFile/ EndSendFile, BeingSendMultipleFiles/ EndSendMultipleFiles and BeginSendFileToUniqueName/ EndSendFileToUniqueName methods of the AsyncFtpClient class are used to send one or more files to an FTP server. 

The SendFile and BeginSendFile/EndSendFile methods send one file to an FTP server while the SendMultipleFiles and BeginSendMultipleFiles/EndSendMultipleFiles methods send one or more files. The SendFileToUniqueName and BeginSendFileToUniqueName/EndSendFileToUniqueName methods also send one file to an FTP server; however, the filename of the remote file will be generated by the FTP server. 

AsyncFtpClient's methods now call the corresponding synchronous methods on a background thread. For this reason, the AsyncFtpClient class is now considered obsolete. It is therefore recommended to use FtpClient instead to improve code readability.
When using Xceed FTP for .NET in a WinForms application, it is recommended that a SynchronizingObject be assigned to the SynchronizingObject property of the FtpClient class. For more information, jump to the WinForms applications and threading topic.  

All the methods above will send the files into the FTP server's current working directory. If you want to send the files to another location, you could use the ChangeCurrentFolder or BeginChangeCurrentFolder/ EndChangeCurrentFolder, or ChangeToParentFolder or BeginChangeToParentFolder/ EndChangeToParentFolder methods to change the current working folder. The current working folder can be retrieved via the GetCurrentFolder or BeginGetCurrentFolder/ EndGetCurrentFolder methods.

Synchronous (blocking) and asynchronous (non-blocking) demonstration using FtpClient

The following example demonstrates how to send multiple files to an FTP server using the SendMultipleFiles method.  An asynchronous (non-blocking) demonstration is also available.

VB.NET Copy Code

Imports Xceed.Ftp

Dim ftp As New FtpClient()

' When using FtpClient, you can instruct
' the library to automatically redirect events on the main UI thread
' by setting the SynchronizingObject property.
ftp.SynchronizingObject = Me

ftp.Connect( "localhost" )
ftp.Login()

ftp.SendMultipleFiles( "d:\*.txt", True, True )

ftp.Disconnect()

C# Copy Code

using Xceed.Ftp;
FtpClient ftp = new FtpClient();

// When using FtpClient, you can instruct
// the library to automatically redirect events on the main UI thread
// by setting the SynchronizingObject property.
ftp.SynchronizingObject = this;

ftp.Connect( "localhost" );
ftp.Login();

ftp.SendMultipleFiles( @"d:\*.txt", true, true );

ftp.Disconnect();

Asynchronous (non-blocking) demonstration

AsyncFtpClient's methods now call the corresponding synchronous methods on a background thread. For this reason, the AsyncFtpClient class is now considered obsolete. It is therefore recommended to use FtpClient and assign a SynchronizingObject to its SynchronizingObject property to improve code readability.

The following example demonstrates how to asynchronously send multiple files to an FTP server using the BeginSendMultipleFiles/ EndSendMultipleFiles methods and provide logging information during the process using the CommandSent and ReplyReceived events.  A synchronous (blocking) demonstration is also available. 

To clarify the code, instead of using callbacks, we will wait for completion of the operation before calling the matching "End" method. More information is available in the WinForms application and threading topic.

VB.NET Copy Code

Imports Xceed.Ftp

Dim ftp As New AsyncFtpClient()
Dim result As IAsyncResult = ftp.BeginConnect("localhost", Nothing, Nothing)

While Not result.IsCompleted
  Application.DoEvents()
End While

ftp.EndConnect( result )
result = ftp.BeginLogin( Nothing, Nothing )

While Not result.IsCompleted
  Application.DoEvents()
End While

ftp.EndLogin( result )
result = ftp.BeginSendMultipleFiles( "d:\*.txt", True, True, Nothing, Nothing )

While Not result.IsCompleted
  Application.DoEvents()
End While

ftp.EndSendMultipleFiles( result )
result = ftp.BeginDisconnect( Nothing, Nothing )

While Not result.IsCompleted
  Application.DoEvents()
End While

ftp.EndDisconnect( result )

C# Copy Code

using Xceed.Ftp;

AsyncFtpClient ftp = new AsyncFtpClient();      
IAsyncResult result = ftp.BeginConnect( "localhost", null, null );

while( !result.IsCompleted )
  Application.DoEvents();

ftp.EndConnect( result );
result = ftp.BeginLogin( null, null );

while( !result.IsCompleted )
  Application.DoEvents();

ftp.EndLogin( result );
result = ftp.BeginSendMultipleFiles( @"d:\*.txt", true, true, null, null );

while( !result.IsCompleted )
  Application.DoEvents();

ftp.EndSendMultipleFiles( result );
result = ftp.BeginDisconnect( null, null );

while( !result.IsCompleted )
  Application.DoEvents();

ftp.EndDisconnect( result );

Things you should consider

  • Are you using Xceed FTP for .NET in a WinForms application? Use the AsyncFtpClient class rather than the FtpClient class. 

  • Do you want the FTP server to initiate the data connection rather than the FTP client? Set the PassiveTransfer property to false. 

  • Do you want the file(s) to be sent in ASCII mode rather than binary? Set the RepresentationType property to ASCII. 

  • Do you want to decrease or increase the period of time after which an FTP operation should timeout? Change the value of the Timeout property. 

  • Do you want to create a log file of the FTP process? Set the TraceWriter property. 

  • Do you want to know the state of the FTP client? Check the Connected and Busy properties. You can also check the State property for specific state information. The StateChanged event can also be used to know when the state of the FTP client changes. 

  • Do you want to know when a file is being sent? Handle the SendingFile event. 

  • Do you want to display progress information? Handle the FileTransferStatus event. 

  • Do you want to continue a multiple-file transfer (when using the SendMultipleFiles method) when one or more of the files being transferred causes an error? Handle the MultipleFileTransferError event. 

  • Do you want to abort the FTP operation? Call the Abort method. 

  • Do you want to prevent routers from prematurely closing the command channel while a long data transfer is taking place. Set the KeepAliveInterval property.