发布于 2015-04-10 04:59:13 | 487 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


问题描述:后台生成了文本文件,用超链接提供给用户下载。点击超链接,下载Excel文件没问题,但文本文件会直接被打开,而不是弹出下载窗口。

解决方法:把HyperLink改为LinkButton,在Click事件中,用文件流写文本文件。

关键代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string sFileName = System.IO.Path.GetRandomFileName();
    string sGenName = "Friendly.txt";

    //YOu could omit these lines here as you may
    //not want to save the textfile to the server
    //I have just left them here to demonstrate that you could create the text file 
    using (System.IO.StreamWriter SW = new System.IO.StreamWriter(
           Server.MapPath("TextFiles/" + sFileName + ".txt")))
    {
        SW.WriteLine(txtText.Text);
        SW.Close();
    }

    System.IO.FileStream fs = null;
    fs = System.IO.File.Open(Server.MapPath("TextFiles/" + 
             sFileName + ".txt"), System.IO.FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    Response.AddHeader("Content-disposition", "attachment; filename=" + 
                       sGenName);
    Response.ContentType = "application/octet-stream";
    Response.BinaryWrite(btFile);
    Response.End();
}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务