
Building FTP Services Using .NET 2.0
by Wei-Meng Lee12/12/2006
.NET 2.0 provides new managed classes for performing FTP accesses: FtpWebRequest
and FtpWebResponse
, which are derived from the WebRequest
and WebResponse
classes, respectively. Using these two new FTP classes, you can now incorporate FTP functionality into your applications easily. In this article, I will show you how to perform the most common FTP functions using these two new classes.
For this article, I will use the FTP service that is provided in Windows XP. My FTP site directory is mapped to C:\Inetpub\ftproot\. Figure 1 shows the initial content of my FTP directory.
Figure 1. The initial content of my FTP directory
All the coding in this article uses the two namespaces below:
Imports System.Net
Imports System.IO
Also, the following constant is defined:
Const ftpURI As String = "ftp://127.0.0.1/"
In this article, I assume that the FTP server is configured for anonymous access and it is installed locally on your computer.
Downloading a Text File
To download a text file using FTP:
- Create an instance of the
FtpWebRequest
class using theCreate()
method of theWebRequest
class. TheCreate()
method takes in a URI parameter (containing the path for the text file to be downloaded). - Set the command to be sent to the FTP server using the
Method
property of theFtpWebRequest
class; in this case this command isDownloadFile
. - Specify the login credential to the FTP server.
- Obtain the response from the FTP server using the
GetResponse()
method from theFtpWebRequest
class. - Retrieve the stream that contains response data sent from an FTP server using the
GetResponseStream()
method from theFtpWebResponse
class.
Note that you can use a StreamReader
object to read the content of the text file:
Try
Dim filename As String = ftpURI & "test.txt"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = FTPResp.GetResponseStream
Dim reader As StreamReader
reader = New StreamReader(ftpRespStream, System.Text.Encoding.UTF8)
Console.WriteLine(reader.ReadToEnd)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Downloading an Image File
If you want to download an image file from an FTP server and then directly bind it to, say, a PictureBox control, you can use the steps outlined above and then use the FromStream()
method from the Image
class to convert the response from the FTP server (containing the image) into an image:
Try
Dim filename As String = ftpURI & "phidgetRFID.jpg"
Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = New NetworkCredential("anonymous", "password")
Dim FTPResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = FTPResp.GetResponseStream
PictureBox1.Image = Image.FromStream(ftpRespStream)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Downloading a Binary File
To download a binary file (in fact, any type of files, including text files) from an FTP server, the easiest way would be to use the WebClient
class.
- First, create a new instance of the
WebClient
class. - Specify the login credential to the FTP server.
- Download the file from the FTP server using the
DownloadData()
method from theWebClient
class. Note that you need to specify the full pathname of the file to be downloaded from the FTP server. - The
DownloadData()
method returns a byte array, which you can save directly onto a file using theWriteAllBytes()
method from theMy.Computer.FileSystem
class.
Try
Dim filename As String = ftpURI & "phidgetRFID.jpg"
Dim client As New WebClient
client.Credentials = New NetworkCredential("anonymous", "password")
My.Computer.FileSystem.WriteAllBytes(".\phidgetRFID.jpg", _
client.DownloadData(filename), True)
MsgBox("File downloaded!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Note that you can also use the DownloadFile()
method from the WebClient
class to download and save a file directly to disk.
Uploading a Binary File
To upload a binary file (including text files) to an FTP server:
- First, create a new instance of the
WebClient
class. - Specify the login credential to the FTP server.
- Upload the file to the FTP server using the
UploadFile()
method from theWebClient
class. Note that you need to specify the full pathname of the file to be uploaded to the FTP server.
Try
Dim filename As String = ftpURI & "PING.bmp"
Dim client As New WebClient
client.Credentials = New NetworkCredential("anonymous", "password")
client.UploadFile(filename, "C:\PING.bmp")
MsgBox("File uploaded!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Note that you can also use the UploadData()
method from the WebClient
class to upload an array of bytes.
Figure 2 shows the contents of my FTP directory after uploading the file PING.bmp.
Figure 2. The contents of my FTP directory after uploading the file
Pages: 1, 2 |
