我有一个部分由用户填充的表,我想将生成的HTML保存到文件中。我正在使用的代码是:
WebClient myClient = new WebClient();
string myPageHTML = null;
byte[] requestHTML;
string currentPageUrl = Request.Url.ToString();
UTF8Encoding utf8 = new UTF8Encoding();
requestHTML = myClient.DownloadData(currentPageUrl);
myPageHTML = utf8.GetString(requestHTML);
string outputFileLocation = currDir + "\\" + "outputFile.html";
FileStream fs = File.Open(outputFileLocation, FileMode.Create, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(myPageHTML);
}
问题似乎是这段代码发送了一个新的页面请求,以便我最终获得初始页面视图而不是用户输入产生的HTML。
感谢任何建议。
问候。
答案 0 :(得分:2)
我会将您的方法放在渲染方法中并将其更改为以下
protected override void Render(HtmlTextWriter writer)
{
sw = new StringWriter();
htmltw = new HtmlTextWriter(sw);
base.Render(htmltw);
StringBuilder html = sw.GetStringBuilder();
string outputFileLocation = currDir + "\\" + "outputFile.html";
FileStream fs = File.Open(outputFileLocation, FileMode.Create, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(html);
}
writer.Write(sOut);
}