Summary
This article describes how to use Active Server Pages to read and display binary data.
Many developers appreciate the ease of using scripting. FileSystemObject in Internet Explorer to open ASCII files and display their content in Microsoft Word or Microsoft Excel. However, at present, ASP does not directly provide any similar objects for reading files that contain binary data, such as Excel worksheets with macros, Adobe Acrobat (.pdf) file. GIF image or any other file that contains binary data. However, ASP developers can write a custom business object or component that adds this function.
More information
"Part I" provides ASP code for receiving and then displaying binary files with the appropriate MIME type, and "Part II" shows how to create Visual Basic 5.0 (or update version) activeX dll component to extend ASP's ability to read binary data.
Part I: ASP example for opening an Excel worksheet with a macro
<%
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, use response. contenttype = "application/pdf" to change the MIME type. For. GIF images, use response. contenttype = "image/GIF ".
Part II: Visual Basic 5.0 ActiveX DLL (myobject. binread)
To create a component that executes the binary READ function, perform the following steps:
Create an ActiveX dll project in Visual Basic 5.0 or later.
Rename this project as myobject.
Rename the class module to binread.
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
Save the project.
Click make myobject. dll in the File menu ".
If the Web server is on a different computer than the one on which you created this component, You need to copy this component to the Web server and register it with regsvr32.
To merge the files created in Part I to another ASP page with text or other formats, use the server-side inclusion statement.
The information in this article applies:
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Microsoft 5.0
Microsoft Visual Basic Enterprise Edition for Microsoft 6.0
Microsoft Active Server Pages