這篇文章將一步一步的說明如何從一個流中得到靜態html頁並把它下載到一個檔案中,當你用一個FileStream對象並把Response.Filter屬性設定到這個FileStream對象時,所有的Response.Write的http輸出都將寫入到檔案中.
1,在visual C#.net 中建立一個名為ASPNETFilter的web公用程式工程'
2,建立右鍵WebForm1.aspx,然後點擊WebForm1.aspx的設計視窗.
3,選擇"源"'
4,用下面的代碼替換掉已有的代碼.
WebForm1.aspx代碼如下:
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ASPNETFilter.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>SaveResponse</title>
</head>
<body>
<form id="SaveResponse" method="post" runat="server">
<asp:TextBox ID="TextBox1" Text="Textbox 1" Runat="server" /><br/>
<asp:ListBox ID="Listbox1" Runat="server" size="3">
<asp:ListItem Value="0">Zero</asp:ListItem>
<asp:ListItem Value="1" Selected="True">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
</asp:ListBox><br/>
<asp:CheckBox ID="Checkbox1" Runat="server" Checked="True" Text="Checkbox 1" />
</form>
</body>
</html>
建立ResponseFilter類:
1,添加一個名為ResponseFilter.cs的新類(實際上是一個新的.cs檔案)
2,用下面的代碼替換掉已有的代碼.
ResponseFilter.cs的如下:
using System;
using System.IO;
namespace ASPNETFilter
{
public class ResponseFilter : Stream
{
private Stream m_sink;
private long m_position;
private FileStream fs;
public ResponseFilter(Stream sink)
{
m_sink = sink;
fs = new FileStream(@"G:/mynet/ASPNETFilter/FilterOutput/response.htm", FileMode.OpenOrCreate, FileAccess.Write);
}
// The following members of Stream must be overriden.
public override bool CanRead
{ get { return true; } }
public override bool CanSeek
{ get { return false; } }
public override bool CanWrite
{ get { return false; } }
public override long Length
{ get { return 0; } }
public override long Position
{
get { return m_position; }
set { m_position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return 0;
}
public override void SetLength(long length)
{
m_sink.SetLength(length);
}
public override void Close()
{
m_sink.Close();
fs.Close();
}
public override void Flush()
{
m_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return m_sink.Read(buffer, offset, count);
}
// Override the Write method to filter Response to a file.
public override void Write(byte[] buffer, int offset, int count)
{
//Write out the response to the browser.
m_sink.Write(buffer, 0, count);
//Write out the response to the file.
fs.Write(buffer, 0, count);
}
}
}
注意:在你運行這個程式之前:
1:建立一個檔案夾G:/mynet/ASPNETFilter/FilterOutput(註:名為ASPNETFilter的web公用程式工程放在"G:/mynet/"目錄下)
2.給這個檔案夾賦於讀與寫的權力(可以設定檔案夾的屬性).
接著如下:
1,在解決方案面板中選擇WebForm1.aspx.
2.按右鍵,選擇查看源碼.
3.在OnInit事件中添寫如何代碼:
Response.Filter = new ResponseFilter(Response.Filter);
WebForm1.aspx.cs代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ASPNETFilter
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
override protected void OnInit(EventArgs e)
{
Response.Filter = new ResponseFilter(Response.Filter);
base.OnInit(e);
}
}
}
測試ResponseFilter
1.儲存改變到ASPNETFilter工程.
2.選擇產生菜單,選擇產生解決方案.