/* * FTP on the Web. This sample demonstrates the use of the PowerTCP Ftp Tool * in the .Net environment. A user can log in, see a file listing, and have a * file displayed in a textbox. * */ using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Ftp { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label lblHost; protected System.Web.UI.WebControls.Label lblTitle1; protected System.Web.UI.WebControls.Label lblUser; protected System.Web.UI.WebControls.TextBox txtUser; protected System.Web.UI.WebControls.Label lblPass; protected System.Web.UI.WebControls.TextBox txtPass; protected System.Web.UI.WebControls.Button cmdLogin; protected System.Web.UI.WebControls.TextBox txtHost; protected System.Web.UI.WebControls.ListBox lstHistory; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label lblFileList; protected System.Web.UI.WebControls.Label lblFile; protected System.Web.UI.WebControls.Button cmdGetFile; protected System.Web.UI.WebControls.Button cmdRefresh; protected System.Web.UI.WebControls.TextBox txtFile; protected System.Web.UI.WebControls.ListBox lstFileList; protected DartFtpDotnet.Ftp Ftp1 = new DartFtpDotnet.Ftp(); // declaration of Ftp component // arraylist to hold history of ftp interaction with server, this will be bound to the display listbox protected System.Collections.ArrayList ArrListHistory = new System.Collections.ArrayList(); protected System.Web.UI.WebControls.Button cmdEndSession; // arraylist to hold listing of files retrieved from server, this will be bound to the file listing listbox protected System.Collections.ArrayList ArrListListing = new System.Collections.ArrayList(); public WebForm1() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); // initialize listHistory ArrListHistory.Add("No info yet, please login"); lstHistory.DataSource = ArrListHistory; lstHistory.DataBind(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.cmdLogin.Click += new System.EventHandler(this.cmdLogin_Click); this.cmdEndSession.Click += new System.EventHandler(this.cmdEndSession_Click); this.cmdGetFile.Click += new System.EventHandler(this.cmdGetFile_Click); this.cmdRefresh.Click += new System.EventHandler(this.cmdRefresh_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void cmdLogin_Click(object sender, System.EventArgs e) { // variables to hold user login info, these will be saved as // session variables and will be updated every time login is clicked string FtpHost; string FtpUser; string FtpPass; FtpHost = txtHost.Text; FtpUser = txtUser.Text; FtpPass = txtPass.Text; // save login info to session variables Session["FtpHost"] = FtpHost; Session["FtpUser"] = FtpUser; Session["FtpPass"] = FtpPass; // clear history ArrListHistory.Clear(); Ftp1.TimeOut = 10000; if(Login()) { // logged in, retrieve a file list and logout RetrieveFileList(true); Logout(); } // bind results to history listbox lstHistory.DataSource = ArrListHistory; lstHistory.DataBind(); } private void Logout() { ArrListHistory.Add("Attempting to Logout of " + Ftp1.RemoteAddress.ToString() + ":" + Ftp1.RemotePort.ToString()); Ftp1.Logout(); ArrListHistory.Add("Logged out"); } private bool Login() { Ftp1.TimeOut = 10000; try { ArrListHistory.Add("Attempting to Login"); // log in using session variables Ftp1.Login(Session["FtpHost"].ToString(), Session["FtpUser"].ToString(), Session["FtpPass"].ToString(), "", 21); ArrListHistory.Add("Logged into " + Ftp1.RemoteAddress.ToString() + ":" + Ftp1.RemotePort.ToString()); return true; } catch(Exception LoginError) { // bad login, display error ArrListHistory.Add("Error Logging In"); ArrListHistory.Add(LoginError.Message); return false; } } private void RetrieveFileList(bool loggedin) { bool blnLoggedIn = loggedin; if(!loggedin) { // not logged in so login. This would be the case if // refresh is called. blnLoggedIn = Login(); } if(blnLoggedIn) { // now everyone is logged in so let's proceed ArrListHistory.Add("Retrieving Listing"); try { // send list command Ftp1.List("-lr"); // iterate through list and add file info to array // this will be bound later to the file listing listbox // add a colon after filename to parse out later for(int i=0; i< Ftp1.Listing.Count; i++) ArrListListing.Add(Ftp1.Listing.Item(i+1).Name + ":" + Ftp1.Listing.Item(i+1).Text); lblFileList.Enabled = true; lstFileList.Enabled = true; cmdGetFile.Enabled = true; cmdRefresh.Enabled = true; cmdLogin.Enabled = false; cmdEndSession.Enabled = true; lblHost.Enabled = false; lblUser.Enabled = false; lblPass.Enabled = false; txtHost.Enabled = false; txtUser.Enabled = false; txtPass.Enabled = false; // bind file listing lstFileList.DataSource = ArrListListing; lstFileList.DataBind(); } catch(Exception ListError) { ArrListHistory.Add("Error Retrieving List"); ArrListHistory.Add(ListError.Message); } } } private void cmdGetFile_Click(object sender, System.EventArgs e) { ArrListHistory.Clear(); Object obj = new object(); string temp = ""; string filename; // parse filename out of selected item in listbox filename = ParseFile(lstFileList.SelectedItem.Text); obj = temp; if(Login()) { try { ArrListHistory.Add("Attempting to retrieve " + filename); // login successful, retrieve file and place results in obj Ftp1.Retrieve(filename, ref obj, 0); ArrListHistory.Add(filename + " successfully retrieved"); txtFile.Visible = true; lblFile.Visible = true; // display obj in textbox and logout txtFile.Text = obj.ToString(); ArrListHistory.Add("Attempting to logout"); Ftp1.Logout(); ArrListHistory.Add("Logged out"); } catch(Exception RetrieveError) { ArrListHistory.Add("Error retrieving file"); ArrListHistory.Add(RetrieveError.Message); } } lstHistory.DataSource = ArrListHistory; lstHistory.DataBind(); } private string ParseFile(string stringIn) { int colon; string temp; // find colon we added earlier and strip all data from the // left side (the filename) colon = stringIn.IndexOf(":", 0, stringIn.Length); temp = stringIn.Substring(0, colon); return temp; } private void cmdRefresh_Click(object sender, System.EventArgs e) { // refresh file list by simply retrieving a new one ArrListHistory.Clear(); RetrieveFileList(false); txtFile.Text = ""; Logout(); lstHistory.DataSource = ArrListHistory; lstHistory.DataBind(); } private void cmdEndSession_Click(object sender, System.EventArgs e) { // since we are not really logged into the ftp server, it isn't // necessary to log out. This button clears the session variables // and allows login under a different username. ArrListHistory.Clear(); ArrListHistory.Add("Ending session for " + Session["FtpUser"].ToString()); Session["FtpHost"] = ""; Session["FtpUser"] = ""; Session["FtpPass"] = ""; cmdLogin.Enabled = true; cmdEndSession.Enabled = false; lblHost.Enabled = true; lblPass.Enabled = true; lblUser.Enabled = true; txtHost.Enabled = true; txtPass.Enabled = true; txtUser.Enabled = true; lblFileList.Enabled = false; lstFileList.DataSource = ""; lstFileList.DataBind(); cmdGetFile.Enabled = false; cmdRefresh.Enabled = false; lblFile.Visible = false; txtFile.Visible = false; ArrListHistory.Add("End Session successful"); ArrListHistory.Add("Please Login or quit"); lstHistory.DataSource = ArrListHistory; lstHistory.DataBind(); } } }