Help and Support

How To Read and Display Binary Data in ASP

Article ID:193998
Last Review:March 16, 2005
Revision:2.4
This article was previously published under Q193998
On This Page

SUMMARY

This article shows how to read and display binary data using Active Server Pages.

Many developers appreciate the ease of using the Scripting.FileSystemObject to open an ASCII file and then display its contents in Microsoft Word or Microsoft Excel from within Internet Explorer. In its current inception, ASP does not directly provide any comparable objects to read files that contain binary data such as an Excel worksheet with macros, an Adobe Acrobat (.pdf) file, a .gif image, or any other files that contain binary data. However, an ASP developer can write a custom business object or component that adds this functionality.

Back to the top

MORE INFORMATION

Part I provides the ASP code that receives and then displays the binary file using an appropriate MIME type, and Part II shows how to create the Visual Basic 5.0 (or later) ActiveX DLL component that extends the capability of ASP to read binary data.

Back to the top

Part I: ASP Sample That Opens an Excel Worksheet Containing Macros

   <%
   Response.buffer = TRUE
   Response.ContentType = "application/x-msexcel"

   Dim vntStream

   Set oMyObject = Server.CreateObject("MyObject.BinRead")
   vntStream = oMyObject.readBinFile("c:\temp\tempxls.xls")

   Response.BinaryWrite(vntStream)

   Set oMyObject = Nothing

   Response.End
   %>
				

NOTE: For Acrobat files, change the MIME type by using Response.ContentType = "application/pdf". For a .gif image, use Response.ContentType = "image/gif".

Back to the top

Part II: The Visual Basic 5.0 ActiveX DLL (MyObject.BinRead)

To create the component that performs the binary read, perform the following steps:
1.Create a new ActiveX DLL project in Visual Basic 5.0 or later.
2.Rename the project MyObject.
3.Rename the class module BinRead.
4.Cut and paste the following code into the General Declarations section of the class module:
   Function readBinFile(ByVal bfilename As String) As Variant
          Dim fl As Long
          Dim FileNum As Long
          Dim binbyte() As Byte
          Dim binfilestr As String

          On Error GoTo errHandler
          
          FileNum = FreeFile
          Open bfilename For Binary Access Read As #FileNum

          fl = FileLen(bfilename)
          ReDim binbyte(fl)

          Get #FileNum, , binbyte

          Close #FileNum

          readBinFile = binbyte
          Exit Function

      errHandler:
          Exit Function
      End Function
						
5.Save the project.
6.On the File menu click Make MyObject.dll.
If your Web server is on a separate machine from where you created the component, you need to copy the component onto the Web server and register it using RegSvr32.

To incorporate the file created in Part I into another ASP page that has text or other formatting, use a server side include statement.

Back to the top


APPLIES TO
Microsoft Visual Basic 5.0 Professional Edition
Microsoft Visual Basic 6.0 Professional Edition
Microsoft Visual Basic 5.0 Enterprise Edition
Microsoft Visual Basic 6.0 Enterprise Edition
Microsoft Active Server Pages 4.0

Back to the top

Keywords: 
kbhowto kbscript kbfso kbcode KB193998

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.