Im able to connect with my sftp server and I'm sure of it because I get the list of files of my server and it passes correct list. But I cant upload file to a folder in mysftp server. Here is my code:
private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword,
string SFTPFilePath, string FileName)
{
Sftp sftp = null;
try
{
sftp = new Sftp( SFTPAddress,SFTPUserName , SFTPPassword);
// Connect Sftp
sftp.Connect();
MessageBox.Show("Connected!");
//Check if im surely connected
//list down files in my sftp server folder u01
ArrayList list;
list = sftp.GetFileList("//u01");
foreach (string item in list)
{
MessageBox.Show(item.ToString());
}
MessageBox.Show(list.Count.ToString());
// upload file
sftp.Put(FileName, "//u01"); -----> **I get exception here**
MessageBox.Show("UPLOADED!");
// Close the Sftp connection
sftp.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (sftp != null)
{
sftp.Close();
}
}
}
I recieve this exception:
"Exception of type 'Tamir.SharpSsh.jsch.SftpException' was thrown."
at Tamir.SharpSsh.jsch.ChannelSftp.put(String src, String dst, SftpProgressMonitormonitor, Int32 mode)
at Tamir.SharpSsh.Sftp.Put(String fromFilePath, String toFilePath)
I've tried using sftp.Put(FileName,SFTPAddress + "//u01");
Ive tried sftp.Put(FileName,SFTPAddress); And it do work but when I look at my sftp server if the file is there, it isn't.
I've tried sftp.Put(FileName,"//u01"); and it throws the same error.
I must upload my file in a folder in my ftp server and one of the folder is u01.
Can anyone help me out. I don't know what's wrong. I'm sure that i'm connected. and when I tried to upload using filezilla it do work so I'm not restricted in writing to our sftp server.
I believe that you have to enter the full filename in your call to Put
.
string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);
Note that StripPathComponent
is not implemented, you will have to implement that as well if needed. It would strip a path component from FileName
, i.e. remove C:\...\
or ..\...\
.