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));
        }
    }

 

posted on 2013-05-30 14:08  小景  阅读(273)  评论(0编辑  收藏  举报