ASP Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang
Sending Binary Files to Browsers
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:
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.
Last update: 2002.
Table of Contents