Cybercrux

Everything is achievable through technology

FTP Access

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Ftps : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ConnectToFtp();
}

}

public FTPHelper.FTPConnection FTP
{
set
{
ViewState[“FTP”] = value;
}
get
{
return ViewState[“FTP”] == null ? null : (FTPHelper.FTPConnection)ViewState[“FTP”];
}
}
public void ConnectToFtp()
{

Dictionary<string, string> Files = new Dictionary<string, string>();
FTP = new FTPHelper.FTPConnection(“10.20.40.100”, “nikhil”, “password”);
Files = FTP.GetFileList(FTP._url);

foreach (string ky in Files.Keys)
{
TreeNode trParent = new TreeNode();
trParent.Text = (string)ky;
trParent.Value = Files[ky];

trVFiles.Nodes.Add(trParent);
}

}
protected void trVFiles_SelectedNodeChanged(object sender, EventArgs e)
{
Dictionary<string, string> _filesInFolder = FTP.GetFileList(trVFiles.SelectedValue);
Dictionary<string, string> _onlyfiles = new Dictionary<string, string>();

foreach (string key in _filesInFolder.Keys)
{

if (key.IndexOf(‘.’) == -1)
{
TreeNode trParent = new TreeNode();
trParent.Text = (string)key;
trParent.Value = _filesInFolder[key];
trVFiles.SelectedNode.ChildNodes.Add(trParent);
}
else
{
_onlyfiles.Add(key, _filesInFolder[key]);
}
}
trVFiles.SelectedNode.Expand();
BindGrid(_onlyfiles);
}
protected void BindGrid(Dictionary<string, string> _files)
{
DataTable dt = new DataTable();
dt.Columns.Add(“FileName”);
dt.Columns.Add(“FolderUrl”);

if (_files != null)
foreach (string key in _files.Keys)
{
DataRow dr = dt.NewRow();
if (key.IndexOf(“/”) > 0)
{
dr[0] = key.Split(‘/’)[1];
}
else
{
dr[0] = key;
}

dr[1] = _files[key];
dt.Rows.Add(dr);
}

grdDetailView.DataSource = dt;
grdDetailView.DataBind();
}

protected void grdDetailView_RowCommand(object sender, GridViewCommandEventArgs e)
{
byte[] _downFile = FTP.DownloadFileFromFtp(e.CommandArgument.ToString());
string _fname = e.CommandArgument.ToString();
string[] sss = _fname.Split(‘.’);

string[] sfname = sss[sss.Length – 2].Split(‘/’);
string ssfname = sss[sss.Length – 2];
Response.ContentType = “application/” + sss[sss.Length – 1];
if (ssfname.IndexOf(“/”) > 0)
{
Response.AddHeader(“Content-disposition”, “attachment; filename=” + sfname[sfname.Length – 1] + “.” + sss[sss.Length – 1]);
}
else
{
Response.AddHeader(“Content-disposition”, “attachment; filename=” + ssfname + “.” + sss[sss.Length – 1]);
}
Response.OutputStream.Write(_downFile, 0, _downFile.Length);
Response.End();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s