显示指定文件夹中的所有文件并提供下载[WEB学习实例]

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class BanZhu_BianLi_WenJian : System.Web.UI.Page
{
    string WeiZhi = @"D:/资源/报告/";//虚拟目录:Files
    string WeiZhi1 = @"D:/公文/";//虚拟目录:Files1

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            string serverpath = Server.MapPath("~/");
            DirectoryInfo dir = new DirectoryInfo(WeiZhi1);
            foreach (FileInfo fileName in dir.GetFiles())
            {
                DataRow dr = dt.NewRow();
                dr[0] = fileName;
                dt.Rows.Add(dr);
            }
            ListBox1.DataSource = dt;
            ListBox1.DataTextField = "Name";
            ListBox1.DataValueField = "Name";
            ListBox1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        if (Directory.Exists(WeiZhi) == false)
        {
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "tixin", "alert('该文件不存在或路径错误!')", true);
        }
        else
        {
            DirectoryInfo di = new DirectoryInfo(WeiZhi);
            FileSystemInfo[] dis = di.GetFileSystemInfos();//.ToString() + Convert.ToString(di.CreationTime);
            if (dis.Length < 1)
            {
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "tixin", "alert('该文件夹是空文件夹!')", true);
            }
            else
            {
                ListBox1.DataSource = dis;
                ListBox1.DataBind();
                //System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "tixin", "alert('检索成功,列表为该路径的文件和目录!')", true);
            }
        }
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["wenjian"] = ListBox1.SelectedValue.ToString();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (Session["wenjian"].ToString() != "" && Session["wenjian"] != null)
        {
            string path = Server.MapPath("~/Files1/") + Session["wenjian"].ToString();//

            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fi.Name));//
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "appilcation/octet-stream;charset=gb2312";
                Response.Filter.Close();
                Response.WriteFile(fi.FullName);
                Response.End();
            }
        }
    }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

<%@ Page Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="BianLi_WenJian.aspx.cs" Inherits="BanZhu_BianLi_WenJian" Title="资料检索" %>


   


       
           
       
       
           
           
           
       
       
           
           
           
       
       
           
           
           
       
       
           
           
           
       
       
           
           
           
       
       
           
           
           
       
   

               

               

                资料检索

               

               

                                    onselectedindexchanged="ListBox1_SelectedIndexChanged">
           

               

               

               
                点击下载
               

               

               

               

               

               

               

               

               

               

               


}

你可能感兴趣的:(显示指定文件夹中的所有文件并提供下载[WEB学习实例])