Many applications require the ability to upload and download files via FTP. Even automated processes regularly interact with FTP servers to transfer data. Recognizing this, Microsoft has given developers a fairly straight forward method to implement this functionality. This document concentrates on showing you the easy way to take advantage of what Microsoft has provided in the .NET Framework.
This blog post is also available in PDF form as a TechRepublic download, which includes a sample Visual Studio file with sample code explaining the techniques outlined.
Preliminary thoughts
Before we get into moving files around I would like to bring a few things to light:
- Each request will need a NetworkCredentials object attached to its Credentials property. This tells the request how to authenticate against the FTP server.
- The URI provided to the request object will include the file name you want to upload or download. For example, if we’re downloading the file “data.xml” from some.ftp.com, our URI will be ftp://some.ftp.com/data.xml.
- You need to be familiar with the Stream object. You’ll use this object both when uploading and downloading files.
These may be simple and you may think “man, who wouldn’t understand that?” Well, when I first started moving files to/from FTP servers I didn’t understand some of these concepts so I thought they would be important! Now let’s get to the code.
Download files
Downloading files is significantly easier than uploading them, so we’ll start out with downloading. What we need to do is setup a WebClient object and set the Credentials property to our login information.
The next step is to call the DownloadData method of the WebClient object and supply the URI of the file we want to download. The DownloadData method returns an array of bytes which represent the downloaded file. This byte array is then written to a file, and the download is complete. The complete code for this is shown in Figure A.
Figure A
Download files
Be aware that the final file.Close() call is crucial. Anytime you open a file in this manner you need to close it or the file will not be accessible by other processes. Closing the stream also commits the changes to disk.
Upload Files
Uploading files is significantly more complicated than downloading files. For one thing, we have to deal with two streams of data. One for the FTP connection and another for the file we’re reading from disk to upload.
These are the steps we take to upload a file:
- Create a FtpWebRequest object
- Set the FtpWebRequest.Method property to UploadFile
- Set the FtpWebRequest.Credentials property to our login information
- Get the request stream of the FtpWebRequest object. This is the stream we’ll write to.
- Open the file we’re going to upload and get a stream to its data
- Read the file’s data from the input stream and write it into the request stream
- Close the uploaded file’s stream and the request stream
The code for this is shown Figure B.
Figure B
Upload files
As you can see it takes over two times as many lines of code to upload than it does to download a file.
Download the sample application
This blog post is also available in PDF form as a TechRepublic download, which includes a sample Visual Studio project file exploring the coding principles outlined. The sample project includes all the code in this post, and an interface to upload/download files that may be useful in your projects.
The interface of the sample project is shown Figure C.