This is a mirror of official site: http://jasper-net.blogspot.com/

Upload file (ftp) using C#

| Monday, April 12, 2010
דוגמת קוד שכתב אחי יוסי גולדברג  להעלאת קבצים ל - ftp

string url = "ftpUrl/FileName";

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user name", "password");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

byte[] buffer = File.ReadAllBytes("path");

using (Stream reqStream = request.GetRequestStream())
{
   reqStream.Write(buffer, 0, buffer.Length);
}

אם לא רוצים לקרוא את כל הקובץ בבת אחת אפשר כמובן לכתוב ככה

using (Stream reqStream = request.GetRequestStream())
{
   int count = 0;
   byte[] buffer = new byte[100];
   using (FileStream file = new FileStream("FileName", FileMode.Open))
   {
       while ((count = file.Read(buffer, 0, 100)) > 0)
       {
           reqStream.Write(buffer, 0, count);
       }
   }
}

Read more: שלמה גולדברג

Posted via email from jasper22's posterous

0 comments: