Go to official website to download http://enterprisedt.com/. NETFTP components
Currently the latest version is 2.2.3, after download, locate EdtFTPnet.dll in the Bin directory and add references to the project.
Using ENTERPRISEDT.NET.FTP;
Public partial class Test_ftp:System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e)
{if (! IsPostBack)
{
Connect_ftp ();
InsertData ();
}
}
#region Connect Ftp private static void Connect_ftp () {
string[] Strarray = null;
ftpclient ftp = new FtpClient ();
var itccconfig = configfactory.getconfig<iitccconfig> ();
Try
{//below to start getting FTP data
Ftp.remotehost = Itccconfig.ftpip;
Ftp.controlport = 21;
Ftp.timeout = 0x2710;
Ftp.connect ();
Ftp.login (Itccconfig.ftpname, Itccconfig.ftppassword);
Ftp.connectmode = FTPCONNECTMODE.PASV;
Ftp.transfertype = Ftptransfertype.ascii;
Ftp.chdir ("/home/ftpuser/itcc/"); Folder path
Strarray = Ftp.dir (".", true);
String strday = DateTime.Now.AddDays (-1). ToString ("YyyyMMdd");
foreach (String strfile in Strarray)
{//if (strfile.length >= 0)
{//string strFileName = strfile.substring (0, 24). TrimStart ();
String strFileName = "Poc_ftp_t04_2015-06-11t0329309.txt";
if (strFileName = = "per" + Strday + ". txt")
{//if (Checkisover (ConnectionString, strFileName, Strday))
{//if (checkisinserting (ConnectionString, strFileName, Strday))
{//string strlogfile = @ "E:\log\upload\copy." + strFileName;
String strlogfile = @ "E:\downftp\copy." + strFileName;
if (file.exists (strlogfile))
File.delete (strLogFile);
Ftp.get (strLogFile, strFileName);
Ftp.get (strFileName); The above is to get the FTP data and store the file on the local hard drive
Insert_date (ConnectionString, NULL, Strday, Strdatainfo, strFilePath + @ "/" + strlogfile);//Data Insert database//} //} //} //} //}
if (ftp.connected)
{ftp.quit ();
}
} catch (Exception ex)
{if (ftp.connected)
{ftp.quit (); }
Throw ex; }
}
#endregion
private static void InsertData () {
FileStream fs = new FileStream ("E:/downftp/copy.poc_ftp_t04_2015-06-11t0329309.txt", FileMode.Open, FileAccess.Read, Fileshare.readwrite);
System.IO.StreamReader my = new System.IO.StreamReader (FS, System.Text.Encoding.UTF8);
String line;
while (line = My. ReadLine ()) = null)
{//response.write (line + "<br/>");
string[] mm = line. Split (';');
Insert (mm); }
My. Close ();
} private static void Insert (string[] mm)
{for (int i = 0; i < mm.) LENGTH/8; i++)
{string employeecode = Mm[i*8];
String employeename = Mm[i*8+1];
String phone1 = Mm[i * 8 + 2];
String phone2 = Mm[i * 8 + 3];
String Phone3 = mm[i * 8 + 4];
string email = mm[i * 8 + 5];
String Department = Mm[i * 8 + 6];
string workplace = Mm[i * 8 + 7];
Employeecommand cmd = new Employeecommand ();
if (cmd. Employyeecode (employeecode) = = True)
{cmd. UpdateEmployee (Employeecode, EmployeeName, Phone1, Phone2, Phone3, Department, workplace, email); }
else {cmd. Createemployee (Employeecode, EmployeeName, Phone1, Phone2, Phone3, Department, workplace, email);
} } }
}
FTP reads TXT data and inserts the database