我用Gridview绑定一些文件的名字,希望点击文件名就可以下载该文件。文件的上传我已经实现,可是很难实现我说的上述下载功能。    <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" CellPadding="4"
        ForeColor="#333333" GridLines="None" Width="603px" DataKeyNames="SoftID" OnRowDeleting="GridView3_RowDeleting">
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:BoundField DataField="SoftID" HeaderText="SoftID" Visible="False">
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:HyperLinkField DataNavigateUrlFields="SoftID" DataNavigateUrlFormatString="Soft.aspx?SoftID={0}"
                DataTextField="SoftName" HeaderText="软件标题">
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" />
            </asp:HyperLinkField>
            <asp:BoundField DataField="SoftContent" HeaderText="软件介绍">
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:HyperLinkField DataNavigateUrlFields="SoftID" DataNavigateUrlFormatString="Soft.aspx?SoftID={0}"
                DataTextField="SoftLogin" HeaderText="下载" />
            <asp:CommandField DeleteText="删除软件" HeaderText="删除软件" ShowDeleteButton="True" />
        </Columns>
        <RowStyle BackColor="#E3EAEB" />
        <EditRowStyle BackColor="#7C6F57" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

解决方案 »

  1.   

    如果是把附件之类的通过FileUpload控件上传到数据库的varbinary(MAX)这样的字段里,那么建议存进数据库之前,通过FileUpload的一些属性,把文件名及文件类型也存进去。然后显示(或下载)时,通过对应的类型修改Reponse的Header,然后就可以Response.BinaryWrite()了。这个可以用ashx处理。
      

  2.   

    麻烦问一下,你这个GridView下载功能实现了吗?我现在也遇到了同样的问题,也是在做毕设,麻烦帮一下忙呗~~~谢谢
      

  3.   

    我也是想实现这个功能,可写好后下载的居然是当前页面,如果用listbox就可以实现
    前台<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                            CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None" 
                            onrowcommand="GridView1_RowCommand">
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <RowStyle BackColor="#EFF3FB" />
                            <Columns>
                                <asp:BoundField DataField="txt" HeaderText="内容" />
                                <asp:ButtonField CommandName="DownLoad" HeaderText="下载" Text="下载" 
                                    DataTextField="Files" />
                            </Columns>
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#2461BF" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
    后台
       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "DownLoad")
            {
                string g = this.GridView1.DataKeys[index].Value .ToString();
                if (g != "")
                {
                    DataTable dt=Class1.GetTxtID(g);
                    DataRowView rv = dt.DefaultView[0];
                    Session["txt"] = rv["Files"].ToString();
                    if (Session["txt"] != null )
                    {
                        
                        string path = Server.MapPath("File/") + Session["txt"].ToString();
                        FileInfo fi = new FileInfo(path);
                        if (fi.Exists)
                        {
                            Response.Clear();
                            Response.AddHeader("Content-Disposition", "attachment;filement=" + Server.UrlEncode(fi.Name));
                            Response.AddHeader("Content-Length", fi.Length.ToString());
                            Response.ContentType = "application/octed-stream";
                            Response.Filter.Close();
                            Response.WriteFile(fi.FullName);
                            Response.End();
                        }
                    }
                }
                else
                {
                    Page.RegisterStartupScript("sb3", "<script>alert('请先选择需要下载的文件,谢谢!')</script>");
                }
            }
            
                
        }