概要
本文介紹了如何使用 Active Server Pages(動態伺服器頁)讀取和顯示位元據。
許多開發人員都很欣賞從 Internet Explorer 中用 Scripting.FileSystemObject 開啟 ASCII 檔案然後在 Microsoft Word 或 Microsoft Excel 中顯示其內容這一方式帶來的方便性。但是在目前,ASP 未直接提供任何類似的對象用以讀取包含位元據的檔案,如帶有宏的 Excel 工作表、Adobe Acrobat (.pdf) 檔案、.gif 映像或任何包含位元據的其他檔案。不過,ASP 開發人員可以編寫一個添加此功能的自訂業務對象或組件。
更多資訊
“部分 I”提供了接收並接著使用合適的 MIME 類型顯示二進位檔案的 ASP 代碼,“部分 II”顯示了如何建立 Visual Basic 5.0(或更新版本)ActiveX DLL 組件,以擴充 ASP 讀取位元據的能力。
部分 I:開啟包含有宏的 Excel 工作表的 ASP 樣本
<%
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
%>
注意:對於 Acrobat 檔案,使用 Response.ContentType = "application/pdf" 更改 MIME 類型。對於 .gif 映像,使用 Response.ContentType = "image/gif"。
部分 II:Visual Basic 5.0 ActiveX DLL (MyObject.BinRead)
要建立執行二進位讀功能的組件,請執行下列步驟:
在 Visual Basic 5.0 或更新版本中建立 ActiveX DLL 項目。
將此項目重新命名為 MyObject。
將類別模組重新命名為 BinRead。
將以下代碼剪下並粘貼到類別模組的“General Declarations”部分:
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
儲存該項目。
在[檔案] 功能表中單擊“Make MyObject.dll”。
如果 Web 服務器在與您建立此組件時所在的電腦不同的另一電腦上,則您需要將此組件複製到 Web 服務器並使用 RegSvr32 註冊它。
要將“部分 I”中建立的檔案合并到具有文本或其他格式的另一個 ASP 頁上,請使用伺服器端包含語句。
這篇文章中的資訊適用於:
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Active Server Pages