首页 > 代码库 > GridView 实现LinkButton下载文件/附件

GridView 实现LinkButton下载文件/附件

 

  <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下载文件/附件