Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

I am facing problem in file uploading through sftp on remote server

$
0
0

I am facing the problem in connecting the port. I am pasting my code here------full code. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using Tamir.SharpSsh;
using Tamir.Streams;
using Org.Mentalis.Security;
using System.IO;
using System.Xml;
using System.Data;
using Tamir.SharpSsh.jsch;
using System.Collections;
using Tamir.SharpSsh.java.net;
using Tamir.SharpSsh.jsch.jce;
using System.IO.Compression;
using Org.Mentalis.Security.Cryptography;
using Org.Mentalis.Security.Ssl;
using Org.Mentalis.Security.Certificates;
using System.Net.Sockets;










namespace ftpmsg
{
    public partial class frmsftpfiletransfer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool result;

           result= FileUploadUsingSftp();
           if (result==true)
           {
               lbl_Msg.Text = "File Uploaded Successfully";
           }
           else
           {
               lbl_Msg.Text = "File not Uploaded Successfully";
           }

        }

       

        public bool FileUploadUsingSftp()
        {
            try
            {

           
            string host = "193.203.230.152";
            int port = 20022;
            string user = "BJ25F";
            string pass = "";
            string privateKey = Server.MapPath("keys/xyz_PrivateKey");
            
            SftpPlugin sftp = new SftpPlugin(host, port, user, pass, privateKey);
                

            // upload a file
            //bool done = sftp.UploadFile(@"D:\Req1.xml", "/");
            bool done = sftp.UploadFile(@"D:\CRYSTAL.txt", txtRemotepath.Text);

            return done;
            }
            catch (Exception)
            {
                
                SftpException.SaveToEventLog("Parameters not accepted Successfully"); 
                return false;
            }
        }

    }

    public class SftpPlugin
    {
        // Private fields
        private int _port;
        private string _privateSshKeyPath;
        private Tamir.SharpSsh.Sftp _tamirSftp;
        
        // Constructors
        public SftpPlugin(string host, string username, string password)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = 20022; // default Port 22
        }

        public SftpPlugin(string host, int port, string username, string password)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = port;   // Specific port
        }

        public SftpPlugin(string host, int port, string username, string password, string privateSshKeyPath)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = port;
            this._privateSshKeyPath = privateSshKeyPath;
        }

        // Private Methods
        private void Connect()
        {
            
            try
            {
                             
            // add the private Key if it is specified
            if (!string.IsNullOrEmpty(this._privateSshKeyPath))
            {
                this._tamirSftp.AddIdentityFile(this._privateSshKeyPath);
               
            }
            this._tamirSftp.Connect(this._port);==============//facing error here --port is not connected.//

            SftpException.SaveToEventLog(" Port Connected successfully");
                
          }
            catch (Exception)
            {

                SftpException.SaveToEventLog(" Port not Connected successfully"); 
            }

        }

        // Public Methods

        // upload a list of files to a remote path
        public bool UploadFiles(string[] files, string toRemotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
                
            }

            try
            {
                this._tamirSftp.Put(files, toRemotePath);
                return true;
            }
            catch (Exception ex)
            {
                
                SftpException.SaveToEventLog(" Files not  Uploaded Successfully"); 
                return false;
                
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // upload a file to a remote path
        public bool UploadFile(string file, string toRemotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Put(file, toRemotePath);
                return true;
            }
            catch (Exception ex)
            {
                
                SftpException.SaveToEventLog(" File not  Uploaded Successfully"); 
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // download a list of files to a local path
        public bool DownloadFiles(string[] files, string toLocalPath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Get(files, toLocalPath);
                return true;
            }
            catch (Exception ex)
            {
               
                SftpException.SaveToEventLog(" Files not  downloaded Successfully"); 
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // download a file to a localPath
        public bool DownloadFile(string file, string toLocalPath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Get(file, toLocalPath);
                return true;
            }
            catch (Exception ex)
            {
                SftpException.SaveToEventLog(" File not Uploaded"); 
                return false;
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // list the files of a remote location
        public string[] ListFiles(string remotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            string[] files = new string[] { };
            try
            {
                files = this._tamirSftp.GetFileList(remotePath).Cast<string>().ToArray();
            }
            catch (Exception ex)
            {


                SftpException.SaveToEventLog(" files of a remote location not shown");
            }
            finally
            {
                this._tamirSftp.Close();
            }
            return files;
        }

       
    }
}



Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>