Asp.net C#将输出捕获到字符串

时间:2013-06-19 03:23:26

标签: c# asp.net httpcontext

我正在写,因为我遇到了问题,我的代码目前将aspx页面的输出写入word文档。这段代码非常好用。但是,我需要将该文件实际保存到服务器。以下是正在运行的代码:

HttpContext.Current.Response.ContentType = "application/msword";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=IR-" + lblReportNumber.Text + ".doc");
HttpContext.Current.Response.Write(this.Page.ToString());

以下是我试图获得所需结果的方法

string fileName = Path.Combine(Server.MapPath("~/"), "IR-" + lblReportNumber.Text + ".doc");
string page = this.Page.ToString();

问题是,写入服务器的 .doc 文件只包含文本的页面名称,而不包含html的完整上下文。

因此,如果我从第二组代码中打开word文档,我看到的只是“clientpage.aspx”,但是第一个代码块会打开完全格式化的单词doc。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

您需要使用StreamWriter

实际写入文件

喜欢

    using (System.IO.StreamWriter file = new System.IO.StreamWriter("filename")
    {
           file.WriteLine()
    }

见这里:http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx