C# ile Dosya Upload
Blog > C# ile Dosya Upload19 Nisan 2010 – 13:09
C# ile uzaktaki server a uplaod yapmak için aşağıdaki fonksiyonu kullanabilirsiniz.Ftp bilgilerinizi girerek ve dosya dizininizi belirterek uplaod yapabilirsiniz..
Kodlar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public string siteAdi = ""; //örn. csharptr.com public string kullaniciAdi = ""; //k.adi public string sifre = ""; //sifre //yukarıda FTP bilgilerinizi giriniz private void Upload(string FtpServer, string Username, string Password, string filename) { FileInfo fileInf = new FileInfo(filename); string uri = "ftp://" + FtpServer + "/klasor/" + fileInf.Name;//Burada uplaod ediceğiniz dizini tam oalrak belirtmelisiniz. FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(Username, Password); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInf.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInf.OpenRead(); try { Stream strm = reqFTP.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Upload Error"); } } |
Eğer bu kodları yaptıktan sonra 550 hatası alıyor iseniz upload ediceğiniz dizini tam olarak belirtmemişsinizdir veya yanlış belirtmişsinizdir.Daha sonra bu fonksyionu çağırarak uplaod işlemini yapabilirsiniz..
Yazan : Mertcan Kurtaran
Etiketler: ”C# ile upload”