分类:
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();