How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP

Article translations Article translations
Article ID: 276488 - View products that this article applies to.
This article was previously published under Q276488
Expand all | Collapse all

SUMMARY

Web developers often need to read binary files from the Web server's file system through Active Server Pages (ASP) and then send the content to the Web browser (for example, to write an Excel file to the browser). Although developers often attempt this with the File System Object (FSO), the FSO is designed to read only ASCII data from the file system and, therefore, does not work.

To read binary data from the file system, you must use a component that has the ability to read binary data. For additional information about to create your own component, click the article number below to view the article in the Microsoft Knowledge Base:
193998 How To Read and Display Binary Data in ASP
In Microsoft ActiveX Data Objects 2.5, the ADODB.Stream object offers this functionality. When you call ADODB.Stream from ASP and use the intrinsic BinaryWrite method from the ASP Response object, you can send binary data to any type of browser with very little code.

MORE INFORMATION

The following steps illustrate how to use this method to write an Excel file to the browser:
  1. Create a new ASP page, and paste the following code:
    <%
    'Set the content type to the specific type that you are sending.
    Response.ContentType = "application/x-msexcel"
    
    Const adTypeBinary = 1
    Dim strFilePath
    
    strFilePath = "C:\ExcelFiles\Excel1.xls" 'This is the path to the file on disk. 
    
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile strFilePath
    
    Response.BinaryWrite objStream.Read
    
    objStream.Close
    Set objStream = Nothing
    %>
    					
  2. Save the file to the Web server.
  3. In the browser, browse to the file.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
248255 How To Use the ADO Recordset, Record and Stream Objects to Open Documents

Properties

Article ID: 276488 - Last Review: July 15, 2004 - Revision: 2.7
APPLIES TO
  • Microsoft Data Access Components 2.5
  • Microsoft Active Server Pages 4.0
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft Visual InterDev 1.0 Standard Edition
  • Microsoft Visual InterDev 6.0 Standard Edition
Keywords: 
kbhowto KB276488

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com