I am connecting to an SFTP server using SSH.NET and trying to read an image into a stream for displaying it in my web page.
SftpClient client = connectToServer(IP,user,pwd);
string remotePath = client.WorkingDirectory.ToString() + dto.Directory + "/";
FileStream fs = new FileStream(remotePath + dto.FileName,FileMode.Open);
Stream strm = fs;
The above code throws an invalid characters exception. I am not sure if the reason for it is because of the filename on the server i.e. all these files are stored with *
delimiter i.e. Text1*Text2*Text3.00.png
.
If it is, is there any other way to read the stream?
I have also tried to read from HttpResponse.OutputStream
but that throws a read/seek invalid exception.
HttpResponse response = HttpContext.Current.Response;
Stream strm = response.OutputStream;
Are there any other ways to read such image files?
This is the working code code for the same functionality using FtpWebRequest
.
var request = (FtpWebRequest)WebRequest.Create(serverUri);
request.UsePassive = true;
request.UseBinary = true;
request.Credentials =//
request.Method = WebRequestMethods.Ftp.DownloadFile;
response = (FtpWebResponse)await request.GetResponseAsync();
Stream responseStream = response.GetResponseStream();
I am changing it to SFTP using SSH.NET.
Is there a way to get the same output as GetResponseStream()
with SSH.NET?
If you need to obtain Stream
API to a file on SFTP server, use SSH.NET SftpClient.Open[Read]
:
Stream fs = client.OpenRead(remotePath);