Thursday, 24 May 2018

Upload file on FTP using C#


1. Open Visual Studio and create a new website

2. Add namespaces like using System.IO; and using System.Net;
3. On Button Click event write below code

string filename = Path.GetFileName(localFile);

                string ftpPath = ftpUrl;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
                ftp.Credentials = new NetworkCredential(Username, Password);

ftp.UsePassive = true;

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
             
                FileStream fs = File.OpenRead(localFile);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();

                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();

4.Run project and upload file.

No comments:

Post a Comment

Thanks

SQL server tricky questions