protected void Page_Load(object sender, EventArgs e)
{
this.WriteToDoc("設定檔", "application/vnd.doc", "內容:我的名字是“靜靜地”");
}
private void WriteToDoc(string FileName, string FileType, string FileTxt)
{
//清除反衝區的內容
Response.Clear();
//設定輸出資料流的HTTP字元集
Response.Charset = "gb2312";
//將一個HTTP頭添加到輸出資料流
HttpBrowserCapabilities hbc = System.Web.HttpContext.Current.Request.Browser;
string browserType = hbc.Browser.ToString().ToLower();
if (browserType == "firefox")
{
Response.AddHeader("Content-Disposition", "attachment;filename=" +FileName+ ".doc");
}
else
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(FileName+".doc")));
}
// Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".doc");
//設定輸出的HTTP MIME類型
Response.ContentType = FileType;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(FileTxt);
//把字元數組寫入HTTP響應輸出資料流
Response.Write(sb.ToString());
//發送完,關閉
Response.End();
}