ASP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.11

Controlling HTTP Response Header Lines

Part:   1  2  3  4 

ASP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

hyBook - Guestbook Application

Using MS Access Databases

ActiveX Data Object (ADO)

Controlling Response Header Lines

Microsoft Scripting Runtime DLL

Using Cookies

ASP Sessions

ASP Objects

Microsoft Script Debugger

Internet Information Services (IIS)

... Table of Contents

(Continued from previous part...)

<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 extention of the requested file is not supported.

Another way of sending files to the browser is via attachment. The following ASP page shows you how to do this:

<script language="vbscript" runat="server">
'  download.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
      response.AddHeader "Content-disposition", "attachment; filename="&p
      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>

In this page, another header line, "Content-disposition", is added to the response, in which I am telling the client program that the entity data is an attachment, with file name specified.

Now try to use IE to request: http://localhost:8080/download.asp?hello.pdf, you will see IE prompting you to save the attachment instead of calling Adobe Reader to display the data.

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2002
ASP Tutorials - Herong's Tutorial Notes - Controlling HTTP Response Header Lines