Uploading files to localhost
1. Make your computer a FTP server.
- i use XAMPP to do this.
- Download XAMPP for Windows. Install it.
- Open the XAMPP control panel from the taskbar notification area or C://Xampp folder
OR
- In the Xampp control panel, click the check boxes before Apache, MySql, FileZilla and click on the 3 start buttons.
- Make sure to click "Install" when asked, when you click on the checkbox before FileZilla.
- Now click on the "Admin" button next to "Stop" button of FileZilla. That will open FileZilla Server control panel.
- Select Edit > Users to create a new user.
- Click the "Add" button under "users" and Type a user name and click OK.
- Type a password in Password field.
- Then click on "Shared Folders" in "Page" box.
- Click "Add" button to add the home directory to use as the location to upload files.
- Select the added path and click the "Set as home dir" button to make it your home directory.
- Check all the check boxes Read, Write, Delete etc next to folders list box.
- Click OK of the Users dialog box and close it.
- Ok now you have a FTP server with a username and a pass.
2. Write the C# code.
- Below is the C# code to upload a test.text file as "voot.txt", to the localhost directory. When the code is executed, the "test.txt" file be copied into the localhost home directory as "voot.txt".
// Get the object used to communicate with the server.
//<FOR LOCALHOST>
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/voot.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
//<FOR LOCALHOST>
request.Credentials = new NetworkCredential("kasun", "mypass");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("test.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
Uploading files to a Remote-Host
1. Register for a free/non-free web host with FTP.
- i have used free hosting site "worlditsme.com" for testing.
- Get your Host (ftp) address, username, password from the site after registering.
- Example Host (ftp) address: ftp://yourname.worlditsme.com/
- User name and password will be the ones that you've used to register the worlditsme.com site.
2. Write the C# code.
Below code will upload "test.txt" file as "voot.txt" to the remote hosts, home folder.
// Get the object used to communicate with the server.
//<FOR REMOTE HOST>
//FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://myusername.worlditsme.com/voo.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
//<FOR REMOTE-HOST>
//request.Credentials = new NetworkCredential("kasunl", "mypass");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("test.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
That's it.
This is a great FTP Library for C#, it is reasonably priced too:
ReplyDeletehttps://www.kellermansoftware.com/p-39-net-ftp-library.aspx