文章出處
文章列表
public void ProcessRequest(HttpContext context) { context.Response.Clear(); context.Response.Buffer = true; //Server.UrlEncode 防止保存的文件名亂碼 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("消費明細" + string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now) + ".txt")); context.Response.ContentType = "text/plain"; string message = "Hello World"; //如果導出的文件要換行,用Environment.NewLine message += "Hello World" + Environment.NewLine; context.Response.Write(message); //停止頁面的執行 context.Response.End(); }
注意兩點:
1.保存文件名亂碼問題:用Server.UrlEncode編碼
2.txt文件中的換行問題:Environment.NewLine
3.調用可以用js:window.location.href="download.ashx" 或window.open("download.ashx")
文章列表
全站熱搜