Upload files via FTP with C#

Prior to going to India, I was looking at using sockets to send data between C# applications and web servers. While the work was interesting and a good insight into the use of NodeJS and various other pieces of the technology, it was unfortunately unable to achieve the task I had been set. This was due to the host I was having to work with not allowing the installation of NodeJS on the server. I'm not quite sure why or what happened as most of it occurred while I was in India. All I came back to was the knowledge that we couldn't achieve what we'd hoped and to look at other options.

Today's post is therefore on one of those options, something which may help others facing similar difficulties in uploading files from C# to web servers where the host won't give you access to NodeJS. This post is a quick introduction to sending files to web servers via FTP instead of sockets.

C# code

The code given here will perform three operations. First it will open a connection to the FTP server using the usual FTP credentials. Then it will transfer the requested file before closing the connection to the server. In the code breakdown there'll be an explanation of the variables to allow you to modify this code to work for you.

You will need to include System.Net and System.IO in order for this code to work.

Code breakdown

Hopefully everything is fairly straight forward. In order for this to work you simply need to provide the information in the table below to the relevant fields.

Field Data Needed
Your_Web.com The URL of your website that you wish to upload to.
Your_Folder The destination folder you wish to upload to (optional)
Your_File The file name you wish to upload to. The file doesn't have to already exist on the server - this simply tells the FTP connection the full path to where it is uploading to, and what the name of the file will be. Remember to include the file extension as well.
Your_Username This is the username to your FTP account that you wish to connect with.
Your_Password This is the password to your FTP account that you wish to connect with.

Leave a Reply

Your email address will not be published. Required fields are marked *