You want to add the ability to download a file from a File Transfer Protocol ( FTP) server at the click of a button (or at any other point in your application) with completely automatic action.
Sample code folder: Chapter 17\FTPDownload
Use the System.Net. FtpWebRequest class to drive the FTP protocol from within your application.
The FtpWebRequest class provides a straightforward way to programmatically download files from FTP servers. This works fine either for anonymous FTP, as shown in this recipe's code, or when using a specific user ID and password.
The following code demonstrates downloading a file from an anonymous FTP server on the Internet. Create a new Windows Forms application, and add a Button control named Button1. Then add the following code to the form's class template:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim sourceFile As String ' ----- Prompt the user for an FTP path. sourceFile = InputBox( _ "Specify a URL for an FTP file to download.") If (sourceFile = "") Then Return ' ----- Initiate the download. DownloadViaFTP(sourceFile, "anonymous", "email@example.com") End Sub
The event handler calls the DownloadViaFTP() method. That method starts by collecting the information it needs, calculating the target output file. sourceFile is the full path to the file to download, located in a folder on a server specifically set up for FTP access. destinationFile is the full path (including the filename) where you want the file to be downloaded, using the same filename as the source file. userName and password are strings containing the credential information to access the FTP server. For anonymous FTP, use "anonymous" for the username. It's customary to use your email address as the password. Here's the method declaration:
Private Sub DownloadViaFTP(ByVal sourceFile As String) ByVal userName As String, ByVal password As String) ' ----- Download the specified file via FTP and save ' it in the application's directory. Dim readBuffer(4095) As Byte Dim count As Integer Dim requestFile As System.Net.FtpWebRequest Dim responseFTP As System.Net.FtpWebResponse Dim responseStream As IO.Stream Dim outFile As IO.FileStream Dim destinationFile As String ' ----- Get the output location. destinationFile = My.Computer.FileSystem.CombinePath( _ My.Application.Info.DirectoryPath, _ My.Computer.FileSystem.GetName(sourceFile))
The variable requestFile is the instance of the FtpWebRequest that we'll use to drive the FTP protocol. Various properties of requestFile, such as Credentials and Method, provide the control required to define the FTP action:
' ----- Connect to the file on the FTP site. requestFile = CType(System.Net.FtpWebRequest.Create( _ sourceFile), System.Net.FtpWebRequest) requestFile.Credentials = New _ System.Net.NetworkCredential(userName, password) requestFile.KeepAlive = False requestFile.UseBinary = True requestFile.Method = _ System.Net.WebRequestMethods.Ftp.DownloadFile
The actual flow of the byes comprising the file to be downloaded is handled by the FtpWebResponse object, which provides a Stream to move the bytes:
' ----- Open a transmission channel for the file content. responseFTP = CType(requestFile.GetResponse, _ System.Net.FtpWebResponse) responseStream = responseFTP.GetResponseStream outFile = New IO.FileStream(destinationFile, _ IO.FileMode.Create)
The stream of bytes is read into a buffer in chunks of up to 4,096 bytes, and from there it's written to the local file:
' ----- Save the content to the output file block by block. Do count = responseStream.Read(readBuffer, 0, _ readBuffer.Length) outFile.Write(readBuffer, 0, count) Loop Until count = 0
Housekeeping wraps up the process:
' ----- Clean up. responseStream.Close() outFile.Flush() outFile.Close() responseFTP.Close() MsgBox("File downloaded!" & vbNewLine & sourceFile) End Sub
By this time, the file has been completely downloaded. To verify that the operation was a success, look in the application folder (wherever the executable file for this program resides) to confirm that the file has been created there.