【问题标题】:How to open the download window when a dynamically created link is clicked in asp.net在asp.net中单击动态创建的链接时如何打开下载窗口
【发布时间】: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 代码。

标签: c# asp.net html download


【解决方案1】:

在您的示例中,您正在生成一个锚标记,该标记具有指向 download javascript 函数的 onclick 处理程序。您不能使用这种方法调用服务器端函数。

解决这个问题的一种方法是write an http handler,它将处理下载给定的文件 id 作为参数。此处理程序将使用文件 id 从数据库中获取文件内容并将其写入响应:

public class Download : IHttpHandler
{
    public void ProcessRequest(System.Web.HttpContext context)
    {
        // read the file name passed in the request
        string fileid = context.Request["fileid"];
        string fileContents = GetFileFromStore(fileid);
        var response = context.Response;
        response.ContentType = "text/plain";
        response.AddHeader("content-disposition", "attachment;filename=abc.txt"); 
        response.Write(fileContents);
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

下一步是生成指向先前创建的通用处理程序的锚点:

<a href="/download.ashx?fileid=1">Download file 1</a>
<a href="/download.ashx?fileid=2">Download file 2</a>
<a href="/download.ashx?fileid=3">Download file 3</a>
...

【讨论】:

【解决方案2】:

首先,正如@Darin Dimitrov 指出的那样,您尝试使用 onclick 处理程序调用服务器方法背后的代码。

在你的情况下,我会使用 ASP:LinkBut​​ton

<asp:LinkButton ID="lnkBtnDownload"runat="server "OnClick="lnkBtnDownload_Click"/>

在后面代码中的事件处理程序中,我会像这样使用 Response.TransmitFile:

 //get the Temp internet folder path
 string filePath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + "\\" + YourFileName;
 //save the file on the server
 FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
 fs.Write(YourByteArray, 0, YourByteArray.Length);
 fs.Close();

 //transmit the file
 Response.ContentType = "application/octet-stream";
 Response.AppendHeader("Content-Disposition", "attachment; filename=" + YourFileName);

 Response.TransmitFile(filePath);
 Response.End();

请注意,上面的代码可以传输任何文件类型,不限于文本文件。

希望对你有帮助

【讨论】:

  • 谢谢你的朋友...我需要知道如何为每个记录获取动态创建链接,即 从数据库而不是放置静态链接按钮
  • 查看这篇关于代码项目的文章,它将帮助你codeproject.com/KB/aspnet/DownloadAssistant.aspx
猜你喜欢
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 2015-10-14
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多