Library code snippets
Sending binary data with ASP
By Lio_889, published on 21 Jun 2002
There are situations where you need to have your ASP page sending binary data to the client. As you might now, there's no direct ASP code for reading a binary file, that's why we need to use an external component for retrieving the binary file.
But instead of writing our own component, why not using the existing ones? The ADO object, which is found on any IIS web server was written to manipulate databases, and since databases are binary files, the ADO object can read our binary files...
<%
Function getBinaryFile(strFilePath)
Dim TypeBinary, oStream
TypeBinary = 1 ' Indicates a binary file
' Create the object
Set oStream = Server.CreateObject("ADODB.Stream")
' Open our file
oStream.Open
' Retreive binary data from the file
oStream.Type = TypeBinary
oStream.LoadFromFile strFilePath
' Return the binary data to the caller
getBinaryFile = oStream.read
' Destroy the ADO object
Set oStream = Nothing
End Function
Response.BinaryWrite getBinaryFile(Server.MapPath("\my_file\thefile.bin"))
%>
Related articles
Related discussion
-
Calling a function from ASP code
by dunk00 (3 replies)
-
GridView HyperLinkField Problem
by Paul2 (0 replies)
-
looking for help on asp
by cladironbeard (2 replies)
-
simple vb to c#, help please
by lksath (1 replies)
-
Binary Studio | software development outsourcing Ukraine
by Hexfinity (2 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
Events coming up
-
Aug
27
Model-View-Presenter (MVC) in ASP.NET
San Francisco, United States
Model-View-Presenter (MVC) in ASP.NET Presenter Clayton Peddy, Terrace Software, Inc. Details TBD
I've altered your code just a little and it works but it doesn't exactly solve my problem. See http://flyinghands.com/a_Download0.asp
This works as long as the file being downloaded is located on the same server as the script. I have so many files I've located them on a seperate hosting service from my web site.
Here is my version:
<%@ LANGUAGE="VBScript" %>
<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->
<%
Function getBinaryFile(strFilePath)
Dim oStream
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = adTypeBinary
oStream.LoadFromFile strFilePath
getBinaryFile = oStream.read
Set oStream = Nothing
End Function
Dim MyFile
Dim MyPath
Dim MyServer
Dim Local
MyPath = "d/lo/10/01/"
MyFile = "d100101.mp3"
Response.AddHeader "content-disposition","inline; attachment; filename=" & chr(34) & Myfile & chr(34)
Response.ContentType = "application/x-unknown"
Response.BinaryWrite getBinaryFile(Server.MapPath(MyPath & MyFile))
%>
The Line Server.MapPath(MyPath & MyFile) resolves to the local server and I need to pass a Remote URL. Any sugestions?
Using the following two articles i managed to get the result I needed ,
http://www.developerfusion.com/show/2542
http://www.developerfusion.com/show/2235
See my other forum article for details ...
http://www.developerfusion.com/forums/topic.aspx?id=11638
Thanks
Pavan
This thread is for discussions of Sending binary data with ASP.