Function readBinFromFile(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 As #FileNum
fl = FileLen(bfilename)
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
readBinFromFile = binbyte
Exit Function
errHandler:
Exit Function
End Function
把上面的代碼編譯成MengXHFileDownLoad.DLL,然後註冊即可使用。下面以直接下載一個When A Man Loves A Woman.mp3的MP3檔案為例子,我們要編寫的ASP指令碼代碼如下:
<%@ Language=VBScript %>
<%
Response.buffer = TRUE
Response.ContentType = "APPLICATION/OCTET-STREAM"
Response.AddHeader "Content-Disposition","attachment;filename=When A Man Loves A Woman.mp3"
Dim varStream, oMyObject
Set oMyObject = Server.CreateObject("MengXHFileDownLoad.BinReadFromFile")
varStream = oMyObject.readBinFromFile("E:\MengXianhui\Mp3\When A Man Loves A Woman.mp3")
Response.BinaryWrite(varStream)
Set oMyObject = Nothing