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
Bloc - The Perfect Placeholder For Your Apple TV
How To Set Up The Ultimate Steam Box (Part 2)
Windows Small Business Server 2011 : Managing User Roles
Letter Of The Month – November 2012 (Part 1)
How To Spellcheck In Any Application
Tablet Group Test – December 2012 (Part 3) : Kurio 7, Samsung Galaxy Tab 2 10.1, Toshiba AT300
AMD And Nvidia Based Products In The New Benchmark (Part 3)
Adobe Premiere CS6 : Time Is Money
Manage Add-Ons For Internet Explorer (Part 1)
For Start-Ups, The Cloud Is The Way To Go!
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered