SharpSSH exception when trying to upload file to SFTP remote server - c#

So this is my code, i basically copied and pasted from SharpSSH website.
Sftp oSftp = new Tamir.SharpSsh.Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
oSftp.Close();
When i run this, i get a "first chance exception" on oSftp.Connect(_Port)
A first chance exception of type 'Tamir.SharpSsh.jsch.JSchException' occurred in Tamir.SharpSSH.dll
Does anyone experienced with SharpSSH have any idea why this is happening? I have also tried uploading files to the server using Rubex but it gave me the same error.

First you need to pass only folder path of server and do not specify file name in that path.
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory);
and also make sure you have added below packages from nuget
Install-Package DiffieHellman
Install-Package Org.Mentalis.Security

just check your path of file and path where you want to upload on sftp.
example:
Sftp sftp=new Sftp("host","username","pass");
sftp.Connect();
sftp.Put("path of your local file","path to upload file on sftp server");
sftp.Close();

Related

Download Excel file from Server C#

I am generating an Excel file in a Hangfire background job in my ASP.NET Core MVC site.
I am running into some issues with getting an excel file to download correctly from my website. When I try to download the file when it is running on localhost, it works completely fine. After the code is published and I try to use the feature on the live version, I can't get the file to download.
string previewName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + content + " - " + Guid.NewGuid() + ".xlsx";
File.Copy(sourceFileName, previewName);
I originally had written this, thinking I would be able to download the file to the client's My Documents folder. I have learned that this is not the case since Environment.GetFolderpath gets the folder path for the servers computer and not the clients. Since the file is hosted on the server, how can I download that file to the client's computer?
Try using a WebClient to download the file from the site.
using System.Net;
string URL = "https://server.com/files/file.txt";
var FilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + content + " - " + Guid.NewGuid() + ".xlsx");
WebClient Client = new WebClient();
Client.DownloadFile(URL, FilePath);
if it is a controller your talking about than instead of copying you can download directly by returning the file in the Controller
return File(yourFilePath);
or you can read the file as bytes and then return it
bytes[] bytesFile = File.ReadAllBytes(path);
return File(bytesFile);

How to connect to external server with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

The following code is what I am trying to use to connect to and external server and get down to the subdirectories in the AppData folder.
string targetPath = #"\\externalServer\" + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Litera\\Customize";
While stepping through the program it comes to line 16 and fails stating "The given path's format is not supported." However, destFile shows "\\externalServer\C:\Users\username\AppData\Roaming\Litera\Customize\Customize.xml". To me the program is finding exactly the path I want. Can someone please help me resolve this problem?
File.Copy(sourceFile, destFile, true);

Deleting the folder and sub folders in FTP server

I had created folders in FTP server with year, month and date that is after getting logged in to the server we can see a folder created on year when I click on that year it shows month and when I click on month it shows date. Now I need to delete this folder.
Below is my code to delete folder in FTP server
FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Can you please help me out to delete a folder.
The URL you assemble for the DeleteFile call is wrong.
With:
path = "ftp://ftp.example.com/" + "/" + ff;
string server = "ftp://ftp.example.com/";
The ftpURL + "/" + ftpDirectory is ftp://ftp.example.com/ftp://ftp.example.com//dir while you want ftp://ftp.example.com//dir or maybe ftp://ftp.example.com/dir.
Use just the ftpDirectory
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpDirectory);
You cannot delete a folder with the WebRequestMethods.Ftp.DeleteFile anyway. You have to use the WebRequestMethods.Ftp.RemoveDirectory.
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
But note that even the .RemoveDirectory can remove an empty directory only.
You have to recursively delete files and subfolders of the folder first and only then you can delete the folder itself.
Implementing a recursion using FtpWebRequest is not easy, particularly because it does not support the MLSD command (what is the only reliable way to distinguish files from folders). For details, see my answer to C# Download all files and subdirectories through FTP.
Alternatively, use another FTP library that supports recursive operations.
For example with WinSCP .NET assembly, you can use Session.RemoveFiles to delete a folder with its content in a single call:
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "username",
Password = "mypassword",
};
using (Session session = new Session())
{
session.Open(sessionOptions);
session.RemoveFiles("/" + ff);
}
(I'm the author of WinSCP)

Move/Rename a file using FtpWebRequest

I am trying to moving a file from one folder to another using FtpWebRequest but i keep getting error 550. This is my code;
var requestMove = (FtpWebRequest)WebRequest.Create(Helper.PathFtp + Helper.NewFolder + file);
requestMove.Method = WebRequestMethods.Ftp.Rename;
requestMove.Credentials = networkCredential;
requestMove.RenameTo = "../" + Helper.OldFolder + file;
requestMove.GetResponse();
I can list, upload, download and delete files but moving/renaming is hopeless. I have read several posts both on stackoverflow and other sites and have tried things like setting Proxy to null and adding special characters to paths but I cant find a solution that works.
The path I use in WebRequest.Create is correct as I can delete it so it must be the RenameTo I got an issue with. Any ideas?
Error 550 means access denied. If the ftp user has sufficient rights, a program (e.g. antivirus, windows thumbnail generator etc) could have the file opened and deny your move request.
You need to contact the server administrator to get around the problem.

C# WebClient Download file from FTP

I am using the following code to download file from FTP.
NetworkCredential credential = new NetworkCredential(Properties.Settings.Default.FTPUserName, Properties.Settings.Default.FTPPassword);
string inputfilepath = Path.Combine(Properties.Settings.Default.LocalDownloadFolder, file);
string ftpfullpath = Properties.Settings.Default.FTPSite + Properties.Settings.Default.FTPFolder + file;
WebClient request1 = new WebClient();
request1.Credentials = credential;
request1.DownloadFile(ftpfullpath, inputfilepath);
Values of the first two vaiables is:
E:\FTPDownloads\CardholderManagementReport_1030_2012-12-11.xls
ftp://abc.com/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
It shows error as :
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
EDIT:
I can see that the file is indeed present there, credentials are ok and I can download it using FileZilla
The error 550 returned by the FTP server indicates that the user you are using to try and access the file with does not have permission to access the file.
Either use some other set of credentials that has access to the file or change the permissions on the file to allow access.
This makes it work.
ftp://abc.com/%2f/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
Explaination is also there.

Categories