gridview中下载附件
1、使用linkbutton,首先把附件的地址绑定到
<asp:TemplateField HeaderText="附件名称"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" Text='<%# Eval("FileTitle") %>' CommandArgument='<%# Eval("saveAddress") %>' OnClick="LinkButton1_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
2、后台代码
protected void LinkButton1_Click(object sender, EventArgs e) { string saveAddress = ((LinkButton)sender).CommandArgument; string path = Server.MapPath(saveAddress); FileInfo fi = new FileInfo(path); string filename = ((LinkButton)sender).Text; if (fi.Exists) { Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename)); } }