Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482677
  • 博文数量: 112
  • 博客积分: 5696
  • 博客等级: 大校
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-17 09:58
文章分类

全部博文(112)

文章存档

2011年(22)

2010年(28)

2009年(21)

2008年(41)

分类:

2008-06-17 15:03:48

   //先拿到datatable,然后用streamwriter写txt文件,最后用responsefile

            DataTable dt = DatabaseProvider.GetInstance().GetBadWords();

            string words = "";

            if(dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count;i++ )
                {
                    words += dt.Rows[i][0].ToString() + " | " + dt.Rows[i][1].ToString() + "\n\r
";
                }
            }
        

            string filename = "words.txt";

            if (File.Exists(BaseConfigs.GetForumPath + filename))
            {
                File.Delete(BaseConfigs.GetForumPath + filename);
            }

            StreamWriter sw = new StreamWriter(filename,true,Encoding.Default);

            sw.Write(words);

            sw.Flush();

            sw.Close();

            byte[] buffer = new Byte[10000];

            Stream stream = new FileStream(filename,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);

            int length = stream.Read(buffer, 0, 10000);

            Response.ContentEncoding = System.Text.Encoding.UTF8;

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));

            HttpContext.Current.Response.ContentType = "text/plain";

            HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.End();

 

另一种方法 txt
Response.Clear();
            Response.Buffer = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName) + ".txt");
            Response.ContentType = "text/plain";
            this.EnableViewState = false;
            Response.Write(sb.ToString());
            Response.End();

阅读(2628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~