Microsoft KB Archive/811162

= HOW TO: Archive the Results of a Dynamic Page to an HTML Page in ASP.NET Using Visual Basic .NET =

Article ID: 811162

Article Last Modified on 5/21/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





For a Microsoft Visual C# .NET version of this article, see 810205.



IN THIS TASK
 * SUMMARY
 * Create a Web Form
 * Create the Response Filter Class
 * Use the Response Filter Class
 * Test the Response Filter
 * REFERENCES



SUMMARY
This step-by-step article describes how to retrieve the HTML results of a page as a stream and then download the stream to a file. When you use the FileStream object and set the Response.Filter property to the FileStream object, all HTTP output that Response.Write sends also downloads as a stream to a file.

back to the top

Create Web Form
To create a Web form:  In Microsoft Visual Studio .NET, create a new Visual Basic .NET ASP.NET Web Application project named ASPNETFilterVB. Right-click the designer pane of WebForm1.aspx. Click View HTML Source to edit the HTML code.  Replace the existing code with the following code : <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;ASPNETFilterVB.WebForm1&quot;%>   SaveResponse   <asp:TextBox ID=&quot;TextBox1&quot; Text=&quot;Textbox 1&quot; Runat=&quot;server&quot; /> <asp:ListBox ID=&quot;Listbox1&quot; Runat=&quot;server&quot; Size=&quot;3&quot;> <asp:ListItem Value=&quot;0&quot;>Zero</asp:ListItem> <asp:ListItem Value=&quot;1&quot; Selected=&quot;True&quot;>One</asp:ListItem> <asp:ListItem Value=&quot;2&quot;>Two</asp:ListItem> </asp:ListBox> <asp:CheckBox ID=&quot;Checkbox1&quot; Runat=&quot;server&quot; Checked=&quot;True&quot; Text=&quot;Checkbox 1&quot; /> </HTML> </li></ol>

back to the top

Create the ResponseFilter Class
To create the ResponseFilter class: <ol> Add a new class named ResponseFilter.vb.</li>  Replace the existing code with the following code: Imports System.IO

Public Class ResponseFilter Inherits Stream Dim fs As FileStream Dim m_sink As Stream Dim m_position As Long

Sub New(ByVal sink As Stream) m_sink = sink fs = New FileStream(&quot;c:\FilterOutput\Response.htm&quot;, FileMode.OpenOrCreate, FileAccess.Write) End Sub 'The following members of Stream must be overridden.

Public Overrides ReadOnly Property CanRead As Boolean Get Return True End Get End Property

Public Overrides ReadOnly Property CanSeek As Boolean Get Return False End Get

End Property

Public Overrides ReadOnly Property CanWrite As Boolean Get Return False End Get End Property

Public Overrides ReadOnly Property Length As Long Get Return 0 End Get End Property

Public Overrides Property Position As Long Get Return m_position End Get Set(ByVal Value As Long) m_position = Value End Set End Property

Public Overrides Function Seek(ByVal offset As Long, ByVal direction As SeekOrigin) As Long Return 0 End Function

Public Overrides Sub SetLength(ByVal length As Long) m_sink.SetLength(length) End Sub

Public Overrides Sub Close m_sink.Close fs.Close End Sub

Public Overrides Sub Flush m_sink.Flush End Sub

Public Overrides Function Read(ByVal buffer As Byte, ByVal offset As Int32, ByVal count As Int32) As Int32 Return m_sink.Read(buffer, offset, count) End Function

Public Overrides Sub Write(ByVal buffer As Byte, ByVal offset As Int32, ByVal count As Int32) ' 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)

End Sub

End Class </li> Create a folder namedC:\FilterOutput.</li> Grant read and write access on the folder for the ASPNET user.</li> Run the Web application.</li></ol>

back to the top

Use the Response Filter Class
<ol> In Solution Explorer, select WebForm1.aspx.</li> Right-click and then select View Code.</li>  Add the following code to thePage_Init event code: Response.Filter = New ResponseFilter(Response.Filter) </li></ol>

back to the top

Test the Response Filter
<ol> Save the changes to the ASPNETFilterVB Web project.</li> On the Build menu, click Build Solution.</li>  Start Microsoft Internet Explorer, and then open WebForm1.aspx by specifying the following URL, where  is the name of your Microsoft Internet Information Services (IIS) server: http://IISServerName/ASPNETFilterVB/WebForm1.aspx </li> Find the Response.htm file in the C:\FilterOutput folder.</li></ol>

back to the top

<div class="references_section">