码迷,mamicode.com
首页 > 其他好文 > 详细

GridView 实现LinkButton下载文件/附件

时间:2016-12-23 09:52:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:ldo   style   btn   where   blog   ppa   and   sql   .text   

 

  <asp:TemplateField  >
           <ItemTemplate>
                 <asp:LinkButton ID="lbtnDownFile" runat="server" CommandName="DownFile" CommandArgument=<%# Eval("UFID") %> >下载</asp:LinkButton>
           </ItemTemplate>
                                   
</asp:TemplateField>

 

protected void gdvwUpFile_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName== "DownFile")
        {
            int id = int.Parse(e.CommandArgument.ToString());

            DataBase db = new DataBase();
            string sqlDown = "select  UFFileName from  PTestUploadFiles where UFID=" + id+"";
            DataTable dt= db.GetDataSet(sqlDown).Tables[0];

            string fileName = dt.Rows[0][0].ToString();
            string filePath = Server.MapPath("/UpFile/"+fileName);
      
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

        }
    }

 

GridView 实现LinkButton下载文件/附件

标签:ldo   style   btn   where   blog   ppa   and   sql   .text   

原文地址:http://www.cnblogs.com/vip-huliji/p/6213587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!