我用Gridview显示数据库中记录,但是有个字段是文件路径,我希望在表格中,文件名直接显示为连接,可以点文件直接下载,应该怎么办?
解决方案 »
- DataBinding:“System.Data.DataRowView”不包含名为“fn”的属性。]
- js 左右拖拽翻页 兼容浏览器问题
- 高分求台球比赛分组算法c#
- 关于网页合成图片的问题,类似于qq秀
- 请高手指点
- 要提高性能,到底什么时候用或者不用ViewState?
- asp.net 断点续传 上传功能如何实现?不要FTP的
- 各位前辈,我要做个ASP.NET项目找工作,可否指点一下
- gridView 怎么样实现固定多层表头
- 用asp.net(C#)开发的WEB应用程序,有的时候打开某个页面的时候,页面没有打开,却显示 Object moved to here. 点击here打开页面,这是什么原
- GridView 根据数据显示不同颜色
- 高手帮忙 动态生成html控件
或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
或者在后台,组合好下载URL的字符串后,直接赋值给这个单元格也行。
DataNavigateUrlFields="userial" DataNavigateUrlFormatString="~/Information/IM_ReceiveInfo.aspx?ID={0}" Target="_self">
<ControlStyle Width="300px" />
</asp:HyperLinkField>protected void GridViewInformationList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{//标题
string cTitle = ((HyperLink)(e.Row.Cells[1].Controls[0])).Text;
((HyperLink)(e.Row.Cells[1].Controls[0])).Text = CommonBusinessService.SubStr(cTitle, 100);
((HyperLink)(e.Row.Cells[1].Controls[0])).ToolTip = cTitle;
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='download.ashx?url=<%# Server.UrlEncode(Eval("FileName").ToString()) %>' ><%#Eval("FileName") %> </a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
建立一个download.ashx
<%@ WebHandler Language="C#" Class="download" %>
using System;
using System.Web;
public class download : IHttpHandler { public void ProcessRequest (HttpContext context) {
string url = HttpContext.Current.Server.UrlDecode(context.Request.QueryString["url"]);
downloadfile(url);
} public bool IsReusable {
get {
return false;
}
}
public void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName; //这里根据实际路径填写
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}
}
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDown" runat="server" CommandName="DownLoad"
Text='<%#Eval("文件名") %>' CommandArgument=<%#Eval("文件路径") %>></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DownLoad")
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string strPath = HttpContext.Current.Server.MapPath("~/") + e.CommandArgument;
System.IO.FileInfo file = new System.IO.FileInfo(strPath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End(); }
}
9 <Columns>
10 <asp:BoundField DataField="UserID" HeaderText="UserID" />
11 <asp:BoundField DataField="C_Name" HeaderText="中文名字" />
12 <asp:BoundField DataField="E_Name" HeaderText="英文名字" />
13 <asp:BoundField DataField="salary" HeaderText="薪水" DataFormatString="{0:C}" HtmlEncode ="False"/>
14 <asp:HyperLinkField DataNavigateUrlFields="C_Name" DataNavigateUrlFormatString="Demo28_Url.aspx?para={0}" DataTextField="C_Name"
15 HeaderText="点击打开新页面" />
16 <asp:HyperLinkField DataNavigateUrlFields="UserID" DataNavigateUrlFormatString="Demo28_Url.aspx?para={0}" DataTextField="C_Name"
17 HeaderText="传递不同的参数(UserID)" />
18 </Columns>
19 <RowStyle HorizontalAlign="Center" />
20 <PagerStyle HorizontalAlign="Right" />
21 </asp:GridView>
请参考GridView控件实现HyperLinkField列打开新页面