file.asp
Open notepad and create a new file. Name it as file.asp. Copy the following
code and paste it into the newly created file.asp file and hit the save
button :
<%
' -- file.asp --
' Retrieves binary files from the database
Response.Buffer = True
' ID of the file to retrieve
Dim ID
ID = Request("ID")
If Len(ID) < 1 Then
ID = 7
End If
' Connection String
Dim connStr
connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("FileDB.mdb")
' Recordset Object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
' opening connection
rs.Open "SELECT [File Name], [File Data], [Content Type] FROM Files " & _
" WHERE ID = " & ID, connStr, 2, 4
If Not rs.EOF Then
Response.AddHeader "Content-Disposition", "filename=" & _
rs("File Name)
Response.ContentType = rs("Content Type")
Response.BinaryWrite rs("File Data")
End If
rs.Close
Set rs = Nothing
%>
Explanation
<%
' -- file.asp --
' Retrieves binary files from the database
Response.Buffer = True
' ID of the file to retrieve
Dim ID
ID = Request.QueryString("ID")
Sets the buffering to True. Next we create a variable named ID
and set it's value to the Request.QueryString("ID").
' Connection String
Dim connStr
connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("FileDB.mdb")
Next we create a connStr variable as the connection string to the database
and set it's path to FileDB.mdb database.
' Recordset Object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
' SQL SELECT Statement
Dim sql_select
sql_select = "SELECT [File Name], [File Data], [Content Type] "
sql_select = sql_select & "FROM Files WHERE ID = " & ID
' opening connection
rs.Open sql_select, connStr, 2, 4
Create a Recordset object and run a SELECT statement to
retrieve the File Name, File Data and Content Type of the given file.
Note that File Data field contains the saved binary data for the file,
while Content Type field contains the content type for the file. We need
both of them to display the file. The "Content-Disposition" header is necessary to send the file with the correct name back to the browser.
If Not rs.EOF Then
Response.AddHeader "Content-Disposition", "filename=" & _
rs("File Name)
Response.ContentType = rs("Content Type")
Response.BinaryWrite rs("File Data")
End If
If Recordset is not empty i.e, there is a file with given ID, then we
set the Content Type of given ASP page to file's Content Type. Next we use
Response.BinaryWrite method to write the binary data to the client browser.
The file will thus be shown to the client.
rs.Close
Set rs = Nothing
%>
We close the connection to the database.
On the next page I summarize the steps involved in displaying binary data from the
database.
Summary
This article was second in the series of articles about manipulating binary data via ASP and storing it in the database. In this article we built files which were missing in the ASP-Database file uploader application in the first article. show.asp simply displayed a list of all the files in the database. file.asp was the actual file which will send the binary data back to the client.
You must have noticed that how easy it is to write binary data from the database via ASP to the client. There are two things to keep in mind while doing that. First, always properly set the Content Type of the page using Response.ContentType property. Secondly, use one ASP page to display only ONE file like we did with file.asp. And that page should not write anything else to the browser i.e, don't try to write text in an ASP page as we do in other ASP pages when you've already used the Response.BinaryWrite method.
Well that's it for this article. You should continue your reading and read the other
two articles of this series; "inserting
binary data to the database" and "uploading
binary data ( files ) to the server hard disk". That'll be enough.