Demir Acar

Demir Acar

  • NA
  • 98
  • 25.2k

how to download ftp files to client directory

Jun 8 2015 6:13 PM
I have a webproject  that run on hosting company , I need to download ftp files to client directory ,my sample code like below ;
 
I think path isn't correct , How CAN I do . 
 
string ftp = "ftp://ftp.adress.com/";
string filepath = "d:\\LEM";
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp+fileName));
reqFTP.Credentials = new NetworkCredential("username","password");
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
//string filePath = (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName));
FileStream writeStream = new FileStream(filepath + "\\" + 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);
}
writeStream.Close();
response.Close();
}
 

Answers (2)