WEBSITE

Upload a File with FTP

9/26/2010 7:44:33 PM
FTP is an old, but ever-popular file transfer mechanism, and .NET supports it out of the box with the WebClient, which will see the “ftp://” part of the URL and internally use a FtpWebRequest to perform the communication.
string host = "ftp.myFtpSite.com";
string username = "anonymous";
string password = "your@email.com";
string file = "myLocalFile.txt";
if (!host.StartsWith("ftp://"))
{
host = "ftp://" + host;
}
Uri uri = new Uri(host);
System.IO.FileInfo info = new System.IO.FileInfo(file);
string destFileName = host + "/" + info.Name;
try
{
//yes, even though this is FTP, we can use the WebClient
//it will see the ftp:// and use the appropriate protocol
//internally
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(username, password);
byte[] response = client.UploadFile(destFileName, file);
if (response.Length > 0)
{
Console.WriteLine("Response: {0}",
Encoding.ASCII.GetString(response));
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}


There is also an asynchronous version of UploadFile.

Other  
 
Most View
Programming WCF Services : Queued Services - Transactions
Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 1) - Loading XNA Textures
Dell Latitude E6430 ATG - All-Day Battery Life
Exchange Server 2010 : Implementing Client Access and Hub Transport Servers - Understanding the Hub Transport Server
Xen Virtualization : Installing Xen from Source
Corsair Carbide 200R – “"Designed For Easy Builds"
Windows 8 Hardware (Part 3) : Acer Iconia W510, Acer T232HL, Innovatouch IW2235P-U
Canon EF 24-70MM F2.8L II USM – A Standard Zoom Many Canon Users Desire
Tablets Buying Guide – April 2013 (Part 1)
GPU Cooler Quick Test: Arctic Accelero Twin Turbo 690 vs. Thermaltake Water 2.0 Pro
Top 10
G-360 And G-550 Power Supply Devices Review (Part 4)
G-360 And G-550 Power Supply Devices Review (Part 2)
Canon IXUS 140 Camera - Great Color Reproduction
Nikon Coolpix S5200 Camera - 10fps Continuous Shooting Mode
Corsair Neutron GTX 240GB - A Fast Performing SSD
G-360 And G-550 Power Supply Devices Review (Part 3)
G-360 And G-550 Power Supply Devices Review (Part 1)
OCZ Vector 256GB - One Of The Dominant Names In SSD
Don’t Pay For Office 2013 (Part 2)
Don’t Pay For Office 2013 (Part 1)