基本信息
源码名称:asp.net 实现ftp文件浏览以及下载(web+ftp)
源码大小:20.90M
文件格式:.zip
开发语言:C#
更新时间:2019-06-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍




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

namespace FTP_Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public FTPHelper.FTPConnection FTP
        {
            set
            {
                ViewState["FTP"] = value;
            }
            get
            {
                return ViewState["FTP"] == null ? null : (FTPHelper.FTPConnection)ViewState["FTP"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnConnect_Click(object sender, EventArgs e)
        {
            ConnectToFtp();
        }

        public void ConnectToFtp()
        {
            try
            {
                FTP = new FTPHelper.FTPConnection(txtFTPUrl.Text.ToLower().Replace("ftp://", ""), txtUser.Text, txtPassword.Text);
                Dictionary<string, string> Files = FTP.GetFileList(FTP._url);
                //string[] files = 
                lblWelcome.Text = "<strong> Welcome </strong> "   txtUser.Text;
                tbLogin.Visible = false;
                foreach (string ky in Files.Keys)
                {
                    TreeNode trParent = new TreeNode();
                    trParent.Text = (string)ky;
                    trParent.Value = Files[ky];

                    trVFiles.Nodes.Add(trParent);
                }
            }
            catch (Exception ex)
            {
                tbLogin.Visible = false;
                lblWelcome.Text = ex.Message;
                lblWelcome.ForeColor = System.Drawing.Color.Red;
            }
        }

        // capturing SelectedNodeChanged even to get the directory and files in selected directory
        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)
            {
                // checking if file is directory 
                if (key.IndexOf('.') == -1)
                {
                    TreeNode trParent = new TreeNode();
                    trParent.Text = (string)key;
                    trParent.Value = _filesInFolder[key];
                    trVFiles.SelectedNode.ChildNodes.Add(trParent); // appending directory to selected parent directory
                }
                else
                {
                    _onlyfiles.Add(key, _filesInFolder[key]); // seperate the files 
                }
            }
            trVFiles.SelectedNode.Expand(); //expand the selected node
            BindGrid(_onlyfiles); // bind the grid with files separated
        }


        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();
                    dr[0] = key;
                    dr[1] = _files[key];
                    dt.Rows.Add(dr);
                }

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

        //So, Finally get RowCommand event to download the desired file
        protected void grdDetailView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            byte[] _downFile = FTP.DownloadFileFromFtp(e.CommandArgument.ToString());
            string _fname = e.CommandArgument.ToString();
            Response.ContentType = "application/"   _fname.Split('.')[1];
            Response.AddHeader("Content-disposition", "attachment; filename="   _fname);
            Response.OutputStream.Write(_downFile, 0, _downFile.Length);
            Response.End();
        }
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            FTP = null;
            tbLogin.Visible = true;
            lblWelcome.Text = "";
            trVFiles.Nodes.Clear();
            BindGrid(null);
        }
    }
}