This question already has answers here:
SFTP Libraries for .NET [closed]
(8 answers)
Closed 6 years ago.
I have created file Upload using FTP (code given below) and it is working Correctly. But now I have to do a file Upload Using SFTP
Kindly request you to guide me on the library to be used for sftp in VS.
class Program
{
static void Main(string[] args)
{
try
{
string path = ConfigurationManager.AppSettings["filepath"];
string[] files = Directory.GetFiles(path, #"*.xlsx");
string FtpServer = ConfigurationManager.AppSettings["ftpurl"];
string Username = ConfigurationManager.AppSettings["username"];
string Password = ConfigurationManager.AppSettings["password"];
string directory = ConfigurationManager.AppSettings["directoryname"];
if (files != null)
{
foreach (string file in files)
{
int port = 22;
FileInfo fi = new FileInfo(file);
string fileName = fi.Name;
string fileurl = path + #"/" + fileName;
string ftpFile = FtpServer + #":" + port + #"/" + directory + #"/" + fileName;
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);
//WebRequest myreq = (WebRequest)WebRequest.Create(ftpFile);
//myreq.Credentials = new NetworkCredential(Username, Password);
myRequest.Credentials = new NetworkCredential(Username, Password);
//WebProxy wb = new WebProxy("http://proxy4.wipro.com:8080");
//wb.Credentials = new NetworkCredential(Username, Password);
//myRequest.Proxy = wb;
myRequest.Method = WebRequestMethods.Ftp.UploadFile;
myRequest.Timeout = 1000000;
myRequest.UseBinary = true;
myRequest.KeepAlive = true;
myRequest.ContentLength = fi.Length;
byte[] buffer = new byte[4097];
int bytes = 0;
int total_bytes = (int)fi.Length;
System.IO.FileStream fs = fi.OpenRead();
System.IO.Stream rs = myRequest.GetRequestStream();
while (total_bytes > 0)
{
bytes = fs.Read(buffer, 0, buffer.Length);
rs.Write(buffer, 0, bytes);
total_bytes = total_bytes - bytes;
}
fs.Close();
rs.Close();
//WebRequest upload = (WebRequest)myreq.GetResponse();
FtpWebResponse uploadResponse = (FtpWebResponse)myRequest.GetResponse();
Console.WriteLine("Upload File Successful");
uploadResponse.Close();
}
}
}
catch (Exception ex)
{
FTPDirectory.logfile.LogInfo("Error in Uploading file in ftp://ftp.xactlycorp.com" + ex.Message);
}
}
}
You can also use http://www.chilkatsoft.com/ssh-sftp-dotnet.asp
Ive used this a lot of stuff and its really good.
There is something named SharpSSH that you can use:
http://www.tamirgal.com/blog/page/SharpSSH.aspx
Example usage:
sftp = new Tamir.SharpSsh.Sftp(ipAddress, username, password);
sftp.Connect();
sftp.Get(sourcePath, destinationPath);
sftp.Close();
Related
While executing the below code I am getting the following error
(The underlying connection was closed The server committed a protocol
violation )
. Please help on this. I almost go throw every article about the error still not getting the solution.
Thanks in Advance.
private static void Upload()
{
string sourceFile = System.IO.Path.Combine(filepath, filename);
string ftpServerIP = "Hostname";
string ftpUserID = "UserName";
string ftpPassword = "Password";
FileInfo fileInf = new FileInfo(sourceFile);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
//try
//{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Until Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
//}
//catch (Exception ex)
//{
// Console.Write(ex.Message, "Upload Error");
//}
}
I found the answer for the problem. I was using SFTP server instead of FTP the code was entirely different.
So I changed the code.
using Renci.SshNet;
public static void UploadSFTPFile()
{
_SftpDetails = ReadIniFile(IniFilePath, "Sftp_Settings", SftpDetails);
string sourcefile = System.IO.Path.Combine(filepath, filename);
string destinationpath = _SftpDetails["SftpDestinationFolder"];
string host = _SftpDetails["Host"];
string username = _SftpDetails["UserName"];
string password = _SftpDetails["Password"];
int port = Convert.ToInt32(_SftpDetails["Port"]); //Port 22 is defaulted for SFTP upload
try
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(destinationpath);
using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(sourcefile));
}
}
}
catch(Exception ex)
{
Console.Write(ex.Message);
return;
}
}
you have to install .sshNet from the NuGet Package Manager
I am trying to Zip files from ftp server(using C#) and save zip file somewhere in my Local pc.I managed a code that could zip a file. But I am not sure how to loop through the ftp to zip all the files (max I should Zip 100 files and Zipfile should not exceed 100 megabytes.
That is the code I used to connect to ftp and Zip 1 file. How I can Zip several Files?
public void ProcessZipRequest(string strQueueID, string strBatchID, string strFtpPath)
{
int intReportCnt = 0;
string strZipFileName = "Order-" + strBatchID + "-" + strQueueID + "-" + DateTime.Now.ToString("MM-dd-yyyy-HH-mm") + ".zip";
strZipFileName = SafeFileName(strZipFileName);
//MemoryStream ms = new MemoryStream();
FileStream ms = new FileStream(#"c:\ZipFiles\nitest.zip", FileMode.Create);
ZipOutputStream oZipStream = new ZipOutputStream(ms); // create zip stream
oZipStream.SetLevel(9); // maximum compression
intReportCnt += 1;
string strRptFilename;
if (strQueueID != null)
{
MemoryStream outputStream = new MemoryStream();
// Get file path
string ftpFilePath = #"ftp://12.30.228.20/AOTest/Images/Report/11/595/45694/62_s.jpg";
// That is where I get 1 file from ftp (How loop here?)
strRptFilename = ftpFilePath.Substring(ftpFilePath.LastIndexOf("/") + 1);
FtpWebRequest reqFTP = (FtpWebRequest)System.Net.WebRequest.Create(ftpFilePath);
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Proxy = null;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Position = 0;
//Where I zip the files
ZipFile(ref outputStream, strRptFilename, ref oZipStream);
ftpStream.Close();
outputStream.Close();
if (response != null)
{
response.Close();
}
}
And that is ZipFile Method that zip the file:
public void ZipFile(ref MemoryStream msFile, string strFilename, ref ZipOutputStream oZipStream)
{
ZipEntry oZipEntry = new ZipEntry(strFilename);
oZipEntry.DateTime = DateTime.Now;
oZipEntry.Size = msFile.Length;
oZipStream.PutNextEntry(oZipEntry);
StreamUtils.Copy(msFile, oZipStream, new byte[4096]);
oZipStream.CloseEntry();
}
Have you considered using a foreach loop with a list.
Try something like
Filenamelist = new List <string>();
Then run a foreach loop to populate the list with your files. Something like
foreach (//listobject in //listname)
{
Filenamelist.Add(file.Name).
}
and from there run the same type of thing with a foreach loop to zip your
function.
Just a thought. Cheers!
I made a post back in january about this, but it was never solved. I have been working on a program to ftp to my Youtube Teams website. For some odd reason, the HTML code generated works and is transferred successfully, but the images come through corrupted. I've made sure that Binary mode is set to true and permissions are all correct. Nothing seems to work.
Can somebody help me?
This is our part of the Code which is relevant to my question:
namespace TMNGP_FTP
{
partial class Form1
{
// some functions and properties for the form
UriBuilder ftpurl;
String ftpurlstr = "ftp://tmngp.heliohost.org";
static String ftpusername = "*******";
static String ftppassword = "*******";
public static string GenerateFileName(string context)
{
return context + DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
}
public void openpic_Click(object sender, System.EventArgs e)
{
//Wrap the creation of the OpenFileDialog instance in a using statement,
//Rather than manually calling the dispose method to ensure proper disposal
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "png files (*.png)|*.png";
if (dlg.ShowDialog() == DialogResult.OK)
{
string folderName = #"c:\TMNGP_Web_Files";
string pathString = folderName + #"\htTemp";
pathString = pathString + #"\imgs";
if (!System.IO.Directory.Exists(pathString))
{
System.IO.Directory.CreateDirectory(pathString);
}
string destFileName = pathString + #"\" + dlg.SafeFileName.ToString();
System.IO.File.Copy(dlg.FileName, destFileName, true);
DisplImg.Image = new Bitmap(dlg.OpenFile());
DisplImg.ImageLocation = destFileName;
}
}
}
private FtpClient ftpnew = null;
public void textgen_Click(object sender, System.EventArgs e)
{
string folderName = #"c:\TMNGP_Web_Files";
string pathString = folderName + #"\htTemp";
if (!System.IO.Directory.Exists(pathString))
{
System.IO.Directory.CreateDirectory(pathString);
}
string fileName = GenerateFileName("HT");
pathString = pathString + #"\" + fileName;
Console.WriteLine("Path to my file: {0}\n", pathString);
if (!System.IO.File.Exists(pathString))
{
//System.IO.FileStream fs = System.IO.File.Create(pathString);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(pathString))
{
file.WriteLine("<div class='simple_overlay' id='news_archive/" + DisplImg.ImageLocation.Substring(31) + "' style='display:none;'>");
file.WriteLine("<a class='close'></a>");
file.WriteLine("<img src='news_archive/" + DisplImg.ImageLocation.Substring(31) + "'/>");
file.WriteLine("<div class='details'>");
file.WriteLine("<h3> " + txtTitle.Text + " </h3>");
file.WriteLine("<h4> " + TxtInfo.Text + " </h4>");
file.WriteLine("<p>" + Desctext.Text + "</p>");
file.WriteLine("</div>");
file.WriteLine("</div>");
}
if(radioButton1.Checked)
{
ftpurl = new UriBuilder("ftp", "tmngp.heliohost.org", 21, "NGP/news_archive/");
ftpurlstr = "/public_html/NGP/news_archive";
}
else
{
ftpurl = new UriBuilder("ftp", "tmngp.heliohost.org", 21, "TM/news_archive/");
ftpurlstr = "/public_html/TM/news_archive";
}
try
{
//string filenametwo = System.IO.Path.GetFullPath(#"c:\TMNGP_Web_Files\htTemp\"+fileName);
string filenamethree = System.IO.Path.GetFullPath(DisplImg.ImageLocation.ToString());
Console.WriteLine("{0}", filenamethree);
Console.WriteLine(pathString);
//string ftpfullpath = ftpurl;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create((#"ftp://tmngp.heliohost.org" + ftpurlstr + fileName).ToString());
Console.WriteLine("{0}", ftpurl + fileName);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
StreamReader sourceStreamone = new StreamReader(#"c:\TMNGP_Web_Files\htTemp\" + fileName);
byte[] fileContentsone = Encoding.UTF8.GetBytes(sourceStreamone.ReadToEnd());
sourceStreamone.Close();
ftp.ContentLength = fileContentsone.Length;
Stream requestStreamone = ftp.GetRequestStream();
requestStreamone.Write(fileContentsone, 0, fileContentsone.Length);
requestStreamone.Close();
FtpWebResponse ftpresponseone = (FtpWebResponse)ftp.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", ftpresponseone.StatusDescription);
ftpresponseone.Close();
}
catch (WebException ex)
{
throw ex;
}
try
{
string imgfile = DisplImg.ImageLocation.Substring(31);
FtpWebRequest ftp2 = (FtpWebRequest)FtpWebRequest.Create((#"ftp://tmngp.heliohost.org" + ftpurlstr + imgfile).ToString());
ftp2.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp2.KeepAlive = true;
ftp2.UseBinary = true;
ftp2.Method = WebRequestMethods.Ftp.UploadFile;
StreamReader sourceStreamtwo = new StreamReader(DisplImg.ImageLocation.ToString());
byte[] fileContentstwo = Encoding.UTF8.GetBytes(sourceStreamtwo.ReadToEnd());
sourceStreamtwo.Close();
ftp2.ContentLength = fileContentstwo.Length;
Stream requestStreamtwo = ftp2.GetRequestStream();
requestStreamtwo.Write(fileContentstwo, 0, fileContentstwo.Length);
requestStreamtwo.Close();
FtpWebResponse ftpresponsetwo = (FtpWebResponse)ftp2.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", ftpresponsetwo.StatusDescription);
ftpresponsetwo.Close();
}
catch (Exception ex)
{
throw ex;
}
MessageBox.Show("FTP Complete");
}
else
{
Console.WriteLine("File \"{0}\" already exists.", fileName);
return;
}
}
// a bunch of Windows Form Designer generated code ...
}
}
Ok this code is hard to read, BUT, it looks like you are using Encoding.UTF8.GetBytes() on the image (sourceStreamTwo) when you declare Byte[] fileContentTwo.
Edit - forgot to mention, you don't need the StreamReader - use a FileStream instead:
FileStream sourceStreamtwo = new FileStream(DisplImg.ImageLocation.ToString(), FileMode.Open);
Change that to
Byte[] fileContentTwo;
using (BinaryReader br = new BinaryReader(sourceStreamtwo))
{
fileContentsTwo = br.ReadBytes((int)sourceStreamtwo.Length);
// rest of code that deals with sourceStreamTwo
}
Note this assumes you aren't reading from a network where you might not have the whole stream available, see Creating a byte array from a stream
In .net 4 or higher you can use Stream.CopyTo() which is safer as it handles interruptions in the stream - again see above question and answers for more info.
And you should be good. Encoding is meant for text, images are binary.
Also please consider some different naming conventions for your variables :)
I have a methode that copy folder structure from ftp to local folder and then copy all files that consists in them:
public void CreateDirectories()
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Url);
request.Credentials = new NetworkCredential(Login, Pass);
request.Method = WebRequestMethods.Ftp.ListDirectory;
string soursePath = #"L:\Test";
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
string directoryName = streamReader.ReadLine();
while (directoryName != null)
{
//Create directories structure
if (directoryName.StartsWith("I") && !directoryName.Contains("p"))
{
string newPath = System.IO.Path.Combine(soursePath, directoryName);
if (!System.IO.Directory.Exists(newPath))
{
System.IO.Directory.CreateDirectory(newPath);
//get file list and invoke DownLoad(string directoryName, string fileName)
FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create(Url + directoryName + "/");
fileRequest.Credentials = new NetworkCredential(Login, Pass);
fileRequest.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader fileStreamReader = new StreamReader(fileRequest.GetResponse().GetResponseStream());
string fileName = fileStreamReader.ReadLine();
while (fileName != null)
{
DownLoad(directoryName, fileName);
fileName = streamReader.ReadLine();
}
}
}
directoryName = streamReader.ReadLine();
}
request = null;
streamReader = null;
}
and the methode that copy current file:
public void DownLoad(string directoryName, string fileName)
{
string localPath = #"L:\Test\" + directoryName;
FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://ftp.equip.me/prod/" + directoryName + "/" + fileName);
requestFileDownload.Credentials = new NetworkCredential(Login, Pass);
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + "\\" + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
responseStream.Close();
writeStream.Close();
requestFileDownload = null;
responseFileDownload = null;
}
But in line Stream responseStream = responseFileDownload.GetResponseStream(); it stop for nearly 40 seconds and then throw an exeption of timeout, and no one file has not been saved (file is small - 50 kb)
The first thing that you should try is to turn passive mode off since this is automatically blocked by most firewalls, but is the default mode of operation for ftpWebRequest.
Just below this line:
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
and this one:
requestFileDownload.UsePassive = false;
Finding some problems copying a zip file from an FTP location. It is just copying and empty file so I think there is something wrong with my use of StreamReader or StreamWriter.
Here is the code:
//read through directory details response
string line = reader.ReadLine();
while (line != null)
{
if (line.EndsWith("zip")) //"d" = dir don't need "." or ".." dirs
{
FtpWebRequest downloadRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftpHost + line); //new Uri("ftp://" + ftpServerIP + DestinationFolder + fileInf.Name));
downloadRequest.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FilesUser"], ConfigurationManager.AppSettings["FilesPass"]);
downloadRequest.KeepAlive = false;
downloadRequest.UseBinary = true;
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
string folderToWrite = HttpContext.Current.Server.MapPath("~/Routing/RoutingFiles/");
string folderToSave = HttpContext.Current.Server.MapPath("~/Routing/");
StreamReader downloadRequestReader = new StreamReader(downloadRequest.GetResponse().GetResponseStream());
DirectoryInfo downloadDirectory = new DirectoryInfo(folderToWrite);
FileInfo file = new FileInfo(Path.Combine(downloadDirectory.FullName, line));
if (!file.Exists)
{
StreamWriter writer = new StreamWriter(Path.Combine(folderToWrite, line), false);
writer.Write(downloadRequestReader.ReadToEnd());
using (var downloadResponseStream = response.GetResponseStream())
{
}
}
}
}
By the time it gets to the bottom of that section, the file has been copied but is empty so I don't think I'm reading the stream correctly for a zip file. Anyone any ideas? I've seen talk of FileStream being better for downloading Zip files, but I couldn't get that to work either.
Thanks.
Here is an example that downloads a file from an ftp.
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddr + "test.zip");
request.Credentials = new NetworkCredential(userName, password);
request.UseBinary = true; // Use binary to ensure correct dlv!
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writer = new FileStream("test.zip", FileMode.Create);
long length = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[2048];
readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
writer.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bufferSize);
}
responseStream.Close();
response.Close();
writer.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Edit I'm sorry for the error in previous code.
When correcting my previous code I found the following resource useful: example