FTP client in ASP.NET

A basic FTP client implemented in ASP.NET.

This ASP.NET sample demonstrates the following features:

  • Connecting to an FTP server from an ASP.NET webpage
  • Persisting FTP connection info between separate page requests
  • Directory browsing
  • Downloading a file from an FTP server and sending it through HTTP to the web browser
  • Uploading files through HTTP to ASP.NET page and resending it to FTP server
  • Creating and deleting directories
  • Resuming downloads

Handling FTP on ASP.NET

Using FTP in ASP.NET environment is a bit tricky due to a fundamental difference in FTP and HTTP protocols. The ASP.NET server communicates with the web browser via HTTP (stateless) protocol while FTP is stateful, session oriented protocol.

Web browser simply connects to the ASP.NET server, fetches a page or other resources and disconnects. Basically, each request/response pair uses a separate connection to the web server, and although HTTP 1.1 makes it possible to reuse a connection for more than one request, the two request/response pairs are pretty much independent.

The FTP client behaves differently. It connects at the beginning, establishing a session. It then uploads or downloads multiple files, creates directories and disconnects at the end - all during the single connection to the FTP server.

When using the FTP protocol from an ASP.NET page, we have two ways of handling it:

  • Use FTP as stateless protocol.

    Connect to FTP, do what needs to be done at the moment and disconnect on each ASP.NET page request. Slower, but safe.
  • Try to maintain FTP session between separate ASP.NET page requests.

    Faster, but brings many problems - e.g.: How to handle users who open dozens of FTP-enabled pages in their browser and never close them? How to store the connected instance of FTP object? Into an ASP.NET Session? What about concurrent access then? And what if the session is persisted into a database? How to handle web farms?

This sample implements the first approach - connect and disconnect during each ASP.NET page request. The user has to enter FTP server name, username and password on first access. This connection info is then stored for later use in the ASP.NET session state.

Uploading a file (Browser -> ASP.NET Server -> FTP Server)

Communication scheme:

  1. Web browser connects to ASP.NET web server and starts sending the file selected using the FileUpload control.
  2. ASP.NET application (at the web server) connects to the FTP server.
  3. ASP.NET application starts receiving the file from the web browser. All received data is immediately sent to the FTP server.
  4. ASP.NET application closes the connection to the FTP server.

Downloading a file (FTP Server -> ASP.NET Server -> Browser)

Communication scheme:

  1. Web browser connects to ASP.NET web server and requests the file download.
  2. ASP.NET application (at the web server) connects to FTP server and requests the file download.
  3. FTP server starts sending the file back to the ASP.NET application.
  4. ASP.NET application starts receiving the file. All received data is immediately sent to the web browser on the client machine.
  5. ASP.NET application closes the connection to the FTP server.