Library code snippets

Sending binary data with ASP


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"))

%>

Comments

  1. 09 Apr 2003 at 00:27

    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?


  2. 17 Jan 2003 at 07:34

    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


  3. 19 Dec 2002 at 14:32
    I tried using oStream.mode=3 (in the article "sending binary data with Asp") but I am unable to open a word document in read/write mode. Does anyone have a solution for this?
    Thanks

    Pavan
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Sending binary data with ASP.

Leave a comment

Sign in or Join us (it's free).

 Lio_889
AddThis

Related discussion

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

We'd love to hear what you think! Submit ideas or give us feedback