Tuesday, October 2, 2012

C#: How to upload files using FTP to a remote server and the localhost



Uploading files to localhost


1. Make your computer a FTP server.

  • i use XAMPP to do this.

  

                    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();

To check whether the file is uploaded, use a client like FileZilla, or the site's home directory by login in with your ftp host address (insert it into your browser and enter your username and pass when prompted.




That's it.

2 comments:

  1. This is a great FTP Library for C#, it is reasonably priced too:
    https://www.kellermansoftware.com/p-39-net-ftp-library.aspx

    ReplyDelete
  2. This is a great FTP Library for C#, it is reasonably priced too:
    https://www.kellermansoftware.com/p-39-net-ftp-library.aspx

    ReplyDelete

https://www.youtube.com/channel/UC6OIDpOAFf4sY3LcydDI5zw?view_as=subscriber