ASP.NET 文件下载

    /// <summary>
    /// 获取服务器上的文件列表
    /// </summary>
    /// <param name="path">服务器的目录路径</param>
    /// <returns>返回一个数据Table</returns>
    public DataTable  GetFileInfo(string path,string filename)
    {
        //新建一个Table用来存储数据
        DataTable mytable = new DataTable();
        DataRow row;
        //创建两个列,一个表示文件名,一个表示文件大小
        DataColumn col = new DataColumn();
        col.DataType = System.Type.GetType("System.String");
        col.ColumnName = "filename";
        DataColumn col2 = new DataColumn();
        col2.DataType = System.Type.GetType("System.Int32");
        col2.ColumnName = "filesize";
        //将两个新列添加到表中。
        mytable.Columns.Add(col);
        mytable.Columns.Add(col2);
        //首先获取服务器目录下的路径
        DirectoryInfo mydir = new DirectoryInfo(path);
        //遍历这个目录下的所有文件
        foreach(FileInfo myfile in mydir.GetFiles(filename))
        {
            //为每行的列赋值
            row=mytable.NewRow();
            row["filename"]=myfile.Name;
            row["filesize"]=myfile.Length /1024;
            //将行添加到Table中
            mytable.Rows.Add(row);
        }
        return mytable;
    }

    this function provider datesource for gridview ,binding gridview

    string path = Server.MapPath("~/FileManager/");
            string filename = ChatRoom.GetPath(id);
            FileManager ma = new FileManager();
            GridView1.DataSource = ma.GetFileInfo(path, filename);
            GridView1.DataBind();

 

download file :

 

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //判断选择的是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //定义选中的控件
            LinkButton btn = (LinkButton)e.Row.Cells[2].Controls[0];
            //为控件添加一个事件参数
            btn.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       // 判断是否是选择的“下载”按钮
        if (e.CommandName == "down")
        {
            //获取控件中的事件参数,并进行类型转换
            int index = Convert.ToInt32(e.CommandArgument);
            // 定义选中的行
            GridViewRow row = GridView1.Rows[index];
            //定义选中的文件的全名
            string SelectName = Server.MapPath("~/FileManager/") + row.Cells[0].Text;
            //获取文件的名字
            string saveFileName = row.Cells[0].Text;

            //创建一个文件实体,方便对文件操作
            FileInfo finfo = new FileInfo(SelectName);
            //清空输出流 
            Response.Clear();
            Response.Charset = "utf-8";
            Response.Buffer = true;
            //关闭ViewState以提高速度
            this.EnableViewState = false;
            //定义输出文件编码及类型和文件名
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment; filename=/"" + Server.UrlEncode(saveFileName) + "/"");
            //  Response.AppendHeader("Content-Disposition", "attachment;filename=" + saveFileName);
            //因为保存的文件类型不限,此处类型选择“unknown”。
            Response.ContentType = "application/unknown";
            Response.WriteFile(SelectName);
            //清空并关闭输出流
            Response.Flush();
            Response.Close();
            Response.End();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值