【发布时间】:2010-04-27 06:22:51
【问题描述】:
我已将 txtfile 存储在数据库中。我需要在单击链接时显示 txtfile。并且必须动态创建此链接。
我的代码如下:
aspx代码:
<div id="divlink" visible="false" runat="server">
</div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataTable dtassignment = new DataTable();
dtassignment = serviceobj.DisplayAssignment(Session["staffname"].ToString());
if (dtassignment != null)
{
Byte[] bytes = (Byte[])dtassignment.Rows[0]["Data"];
//download(dtassignment);
}
divlink.InnerHtml = "";
divlink.Visible = true;
foreach (DataRow r in dtassignment.Rows)
{
divlink.InnerHtml += "<a href='" +
"'onclick='download(dtassignment)'>" +
r["Filename"].ToString() + "</a>" + "<br/>";
}
}
}
-
public void download(DataTable dtassignment)
{
System.Diagnostics.Debugger.Break();
Byte[] bytes = (Byte[])dtassignment.Rows[0]["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dtassignment.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename="
+ dtassignment.Rows[0]["FileName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
我已经动态获得了链接,但是当我点击链接时我无法下载 txt 文件。如何执行此操作。请帮帮我...
【问题讨论】:
-
请详细说明“但是当我单击链接时我无法下载 txt 文件”是什么意思。另外,请重新格式化您的代码。
-
您的 ASPX 代码仍然不可见。尝试重新格式化。
-
@the Villahe idiot .. 删除了我的格式 ;-) 他也删除了 aspx 代码。