Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a windows Application from Which i need to upload a generated excel File To SFTP.
my SFTP url is Like https://sftp.k.com

Please let me know if dotnet have inbuilt feature to upload to SFTP or do we need To use any third party tool.

Please guide me with the steps in any of the possible way.

thanks in advance
Posted
Updated 2-Jul-13 23:46pm
v2

1 solution

I recently had to upload files to my server using sftp as well. To my surprise, you can actually use the System.Net .net libraries to do this. System.net contains FtpWebRequest and FtpWebResponse classes which prove to be very helpful.

Uploading might look like this:
C#
/* Upload File */
        public void upload(string remoteFile, string localFile, string host, string user, string pass)
        {
            try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                /* Establish Return Communication with the FTP Server */
                ftpStream = ftpRequest.GetRequestStream();
                /* Open a File Stream to Read the File for Upload */
                FileStream localFileStream = new FileStream(localFile, FileMode.Open);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];
                int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
                try
                {
                    while (bytesSent != 0)
                    {
                        ftpStream.Write(byteBuffer, 0, bytesSent);
                        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return;
        }


and can be used like this:

upload("/public_html/testUpload.txt", @"C:\Users\1161074\Documents\testUpload.txt", @"ftp://xx.xxx.xxx.xx/", "username", "password");


I suggest making a little class out of this to have upload/download, etc. Thats what I did. Let me know if you would like it.
 
Share this answer
 
Comments
samitrpatel 31-Mar-15 8:52am    
please dude there is a wrong solution it is use for ftp not for sftp my dear friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900