Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

For a Microsoft Visual C# .NET version of this article, see 810205 (http://support.microsoft.com/kb/810205/EN-US/ ) .
For a Microsoft Visual C# .NET version of this article, see 810205 (http://support.microsoft.com/kb/810205/EN-US/ ) .

↑ Back to the top


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.

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>
    

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.

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)

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:
HttpResponse.Filter Property

↑ Back to the top


Keywords: KB811162, kbfileio, kbwebforms, kbhowto

↑ Back to the top

Article Info
Article ID : 811162
Revision : 7
Created on : 5/21/2003
Published on : 5/21/2003
Exists online : False
Views : 432