🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / Ftp dosya kaydetme ve ftp dosya indirme

1-) C# RMOS - Ftp dosya kaydetme ve ftp dosya indirme

 

kaynak : https://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp

 

NOT :

 

System.Net.WebException: 'Uzak sunucu hata döndürdü: (553) Dosya adına izin verilmiyor.'

HATASI ALIRSAN

 

ftp://domain.com/out/ yerine aşağıdaki gibi tam yolu yazıcaksın

 

ftp://domain.com/domains/domain.com/public_html/out/

 

 

1-) FTP USER VE PASSWORD ile dosya kaydetme

 

private void button1_Click(object sender, EventArgs e)

        {

            ftpKaydet(@"C:\cc\ss.gif","ss.gif");

        }

 

 

public void ftpKaydet(string path,string fileName)

        {

            try

            {

                using (var client = new WebClient())

                {

                    client.Credentials = new NetworkCredential(txtFtpUser.Text, txtFtpPassword.Text);

                    client.UploadFile("ftp://"+ "193.53.500.50:2121" + "/" + fileName,                                                   WebRequestMethods.Ftp.UploadFile, path);

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show("HATA ftpKaydet() "+ex.Message);

            }

        }

 

 

2. diger

 

NuGet\Install-Package SSH.NET -Version 2020.0.2

 

 

public void ftpKaydet()

        {

            try

            {

                using (SftpClient client = new SftpClient("252.138.4.52", 22, "k.ad", "şifre"))

                {

                    client.Connect();

                    client.ChangeDirectory("/var/www/html/yol");

                    using (FileStream fs = new FileStream(@"C:\cc\deneme.jpg", FileMode.Open))

                    {

                        client.BufferSize = 4 * 1024;

                        client.UploadFile(fs, Path.GetFileName(@"C:\cc\deneme.jpg"));

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("HATA ftpKaydet() " + ex.Message);

            }

        }

 

2-) DİĞER

 /*

       files_id -> örn -> 3

       ftpTamYol -> örn -> ftp://222.11.52.75/DOSYA/Sancaktepe/

       ->  kullanımı ->  ftpEvrakKaydet( "ftp://222.11.52.75/DOSYA/Sancaktepe/", files_id);

        */

        public void ftpEvrakKaydet(string ftpTamYol, string files_id) //

        {

            try

            {

                using (OpenFileDialog dlg = new OpenFileDialog())

                {

                    dlg.Title = "Open Image";

                    //dlg.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";

                    if (dlg.ShowDialog() == DialogResult.OK)

                    {

                        string fileName = dlg.FileName;

                        string dosyaUzanti = Path.GetExtension(fileName);

 

                        string dosyaAd = files_id + dosyaUzanti;

 

                        FileInfo filei = new FileInfo(fileName);

                        string path = ftpTamYol + dosyaAd;

                        FtpWebRequest FTP;

                        FTP = (FtpWebRequest)FtpWebRequest.Create(path);

                        FTP.KeepAlive = false;

                        FTP.Method = WebRequestMethods.Ftp.UploadFile;

                        FTP.UseBinary = true;

                        FTP.ContentLength = filei.Length;

                        int buffLength = 1024;

                        byte[] buff = new byte[buffLength];

                        int contentLen;

                        FileStream FS = filei.OpenRead();

                        Stream strm = FTP.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)

            {

                MessageBox.Show("Yükleme yapılamadı");

            }

        }

 

public void ftpEvrakIndir()

        {

            string ftpDosyaTamYol = "ftp://222.11.75/DOSYA/Sancaktepe/11.pdf";

 

            SaveFileDialog savefile = new SaveFileDialog();

            savefile.FileName = Path.GetFileName(ftpDosyaTamYol); ;

 

            if (savefile.ShowDialog() == DialogResult.OK)

            {

                string nereye = savefile.FileName;

                WebClient client = new WebClient();

                //client.Credentials = new NetworkCredential("username", "password");

                client.DownloadFile(ftpDosyaTamYol, nereye);

            }

        }

 2025 Mayıs 24 Cumartesi
 1,379