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:
- 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="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:
- Add a new class named ResponseFilter.vb.
- 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
- Create a folder namedC:\FilterOutput.
- Grant read and write access on the folder for the ASPNET
user.
- Run the Web application.
Use the Response Filter Class
- In Solution Explorer, select
WebForm1.aspx.
- Right-click and then select View
Code.
- Add the following code to thePage_Init event code:
Response.Filter = New ResponseFilter(Response.Filter)
Test the Response Filter
- Save the changes to the ASPNETFilterVB Web
project.
- On the Build menu, click Build
Solution.
- 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
- Find the Response.htm file in the C:\FilterOutput
folder.