This section provides a tutorial example on how to send files to the Web browser in binary mode - using response.ContentType to set the correct file type, and using response.BinaryWrite() method deliver the file content.
Sometimes, you may want to send to the browser files that are
not in the HTML format, for example, a PDF document, or MS Word Document. In this
case, we have to set Content-Type, Content-Length and other header lines carefully
to provide correct information about the entity body for the client program.
Another challenge of sending these types of files is that they must be handled
in binary mode. The TextStream class offered in c:\winnt\system32\scrrun.dll
can not be used to do this. You must use the Stream class offered in
c:\program files\common files\system\ado\msadox.dll, known as the ADO DLL.
Here is a sample ASP page to show you how to set header lines for different types
of data in the entity body.
<script language="vbscript" runat="server">
' get_file.asp
' Copyright (c) 2002 by Dr. Herong Yang
p = request.ServerVariables("QUERY_STRING")
ok = Len(p)>0
if (ok) then
if (InStr(p,".html")>0) then
contentType = "text/html"
elseif (InStr(p,".gif")>0) then
contentType = "image/gif"
elseif (InStr(p,".pdf")>0) then
contentType = "application/pdf"
elseif (InStr(p,".doc")>0) then
contentType = "application/msword"
else
ok = false
end if
end if
r = request.ServerVariables("APPL_PHYSICAL_PATH")
filePath = r & p
if (ok) then
response.Buffer = True
response.Clear
response.ContentType = contentType
set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'binar type?
objStream.LoadFromFile filePath
response.BinaryWrite objStream.Read
objStream.Close
response.Flush
response.End
else
response.Write "<html><body>Error:<br/>"
response.Write "ApplPhysicalPath = ("&r&")<br/>"
response.Write "QueryString = ("&p&")<br/>"
response.Write "</body></html>"
response.End
end if
</script>
Ideas used in this page:
The objective of this page is to send back the content of the requested file
in entity body, and set the Content-Type correctly.
The requested file name is given in the query string of the HTTP request.
The extension of the requested file name is checked to determine the Content-Type
header line.
Then the requested file is opened with "ADOBD.Stream", and the content is copied to output stream
of the response object in binary mode.
The Content-Length header line is set by IIS correctly.
Now let's see how this page works.
1. Use IE (Internet Explorer) to request: http://localhost/get_file.asp?hello.html,
you should see the hello message properly displayed as HTML document.
2. Use IE to request: http://localhost/get_file.asp?dot.gif,
you should see a tiny dot displayed as an image.
3. Use IE to request: http://localhost/get_file.asp?hello.pdf,
you should see IE calling Adobe Reader to display the hello message as a PDF document.
4. Use IE to request: http://localhost/get_file.asp?hello.doc,
you should see IE calling MS Word to display the hello message as Word document.
Of course, you have to prepare such a Word document and put it on the IIS server in
order to do this test.
5. Use IE to request: http://localhost/get_file?any.txt,
you should see IE displaying an error message. The reason is, of course, that
the file name extension of the requested file is not supported.