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

%>

You might also like...

Comments

 Lio

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie