
Aug 21st, 2003, 14:34
|
|
Junior Member
|
|
Join Date: Aug 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Send Binary Data
Just finished writing this little function to send binary files that live in non web accessable folders for a client. Enjoy
- Code: Select all
'Function Written by Pedro S.
function SendBinaryFile(fPath)
dim fName, stream, binData, fso, fileLen
'If not physical path, then make it *********************
if instr(fPath,":") = 0 then fPath = Server.MapPath(fPath)
fName = Right(fpath,len(fpath) - inStrRev(fPath,"\"))
'Check if File Exists and Get its File Size -----------
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(fPath) then
Response.Write "File Not Found: " & fName
Response.End
end if
fileLen = fso.GetFile(fPath).size
set fso = nothing 'end --------------------------------
'Get all the Binary Data ------------------------------
on error resume next
set stream = Server.CreateObject("ADODB.Stream")
stream.Open()
stream.Type = 1 'Binary
stream.LoadFromFile(fPath)
binData = stream.read
stream.Close()
set stream = nothing
'Error Handle Opening and Reading the File.
if not err = 0 then
Response.write "Error opening/reading file."
Response.End
end if
on error goto 0 'end ----------------------------------
'Prepare to Send File *********************************
Response.Buffer = True
Response.Clear
Response.AddHeader "content-disposition", "attachment; filename=" & fName
Response.AddHeader "Content-Length", fileLen
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
'Send File *******************************************
Response.BinaryWrite(binData)
Response.Flush
binData = nothing
end function
|