如何GridView中点击链接实现下载功能

xiu_555222 2009-04-12 12:25:52
我用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>
...全文
948 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhucezhenmafan 2010-08-12
  • 打赏
  • 举报
回复
我也是想实现这个功能,可写好后下载的居然是当前页面,如果用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>");
}
}


}
batistutaluke 2010-05-06
  • 打赏
  • 举报
回复
麻烦问一下,你这个GridView下载功能实现了吗?我现在也遇到了同样的问题,也是在做毕设,麻烦帮一下忙呗~~~谢谢
namhyuk 2009-04-12
  • 打赏
  • 举报
回复
如果是把附件之类的通过FileUpload控件上传到数据库的varbinary(MAX)这样的字段里,那么建议存进数据库之前,通过FileUpload的一些属性,把文件名及文件类型也存进去。

然后显示(或下载)时,通过对应的类型修改Reponse的Header,然后就可以Response.BinaryWrite()了。这个可以用ashx处理。
xiu_555222 2009-04-12
  • 打赏
  • 举报
回复
大家帮帮忙啊,我实在是想不出什么好的办法。。。在弄毕业设计啦。很急。。。最好有源码。。。谢谢了。

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧