Bir önceki makalede FTP'nin ne olduğunu inceleyip FTP ile dosya gönderme işlemini gerçekleştirmiştik.
Şimdi ise FTP ile sunucudan dosya alma işlemini inceleyelim.
Dosya alma işlemi için C# kodları ve açıklamaları aşağıdadır.
public string FTPdenDosyaIndir(string dosya,string klasor)
{
FtpWebRequest ftpIstegi;
try
{
FileStream stream = new FileStream(klasor + "\\" + dosya.Substring(dosya.LastIndexOf('\\')), FileMode.Create);
//Alınacak dosyayı bul
ftpIstegi = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dosya.Substring(dosya.LastIndexOf('\\'))));
// Yapılacak işlem (Download)
ftpIstegi.Method = WebRequestMethods.Ftp.DownloadFile;
//Verinin alım şekli.
ftpIstegi.UseBinary = true;
//Kullanıcı adı ve şifre
ftpIstegi.Credentials = new NetworkCredential("deneme@ugurkizmaz.com", "123s");
FtpWebResponse yanit = (FtpWebResponse)ftpIstegi.GetResponse();
Stream ftpStream = yanit.GetResponseStream();
//Buffer uzunluğu
int bufferUzunlugu = 2048;
byte[] buffer = new byte[bufferUzunlugu];
int okumaSayisi = ftpStream.Read(buffer, 0, bufferUzunlugu);
while (okumaSayisi > 0)
{
stream.Write(buffer, 0, okumaSayisi);
okumaSayisi = ftpStream.Read(buffer, 0, bufferUzunlugu);
}
ftpStream.Close();
stream.Close();
yanit.Close();
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
Veysel Uğur KIZMAZ
Bilgisayar Mühendisi
veysel@ugurkizmaz.com
www.ugurkizmaz.com