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



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework 1.1)
  • Microsoft ASP.NET (included with the .NET Framework) 1.0
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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

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:
  1. In Microsoft Visual Studio .NET, create a new Visual Basic .NET ASP.NET Web Application project named ASPNETFilterVB.
  2. Right-click the designer pane of WebForm1.aspx.
  3. Click View HTML Source to edit the HTML code.
  4. Replace the existing code with the following code :
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ASPNETFilterVB.WebForm1"%>
    <HTML>
    	<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>
    

back to the top

Create the ResponseFilter Class

To create the ResponseFilter class:
  1. Add a new class named ResponseFilter.vb.
  2. 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("c:\FilterOutput\Response.htm", 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
    
  3. Create a folder namedC:\FilterOutput.
  4. Grant read and write access on the folder for the ASPNET user.
  5. Run the Web application.
back to the top

Use the Response Filter Class

  1. In Solution Explorer, select WebForm1.aspx.
  2. Right-click and then select View Code.
  3. Add the following code to thePage_Init event code:
    Response.Filter = New ResponseFilter(Response.Filter)
back to the top

Test the Response Filter

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

REFERENCES

For more information, visit the following Microsoft Developer Network (MDSN) Web site: back to the top

Modification Type:MajorLast Reviewed:5/21/2003
Keywords:kbhowto kbWebForms kbFileIO KB811162 kbAudITPRO kbAudDeveloper