Send Binary Data from ASP

ASP Modifications

Now that we have access to the byte array of data from within ASP, it is a staightforward process to use it.

<%
Response.Buffer = True
Response.Clear
Response.AddHeader "content-disposition", "attachment; filename=" & FileName
Response.ContentType = "application/pdf"
vStream = oUtility.ReadBinaryFile(Server.MapPath(PathName))

Response.BinaryWrite(vStream)

Set oUtility = Nothing

Response.End
%>


Since we are adding headers to the Reponse object, it is important that nothing be written to the Response object before we use the AddHeader method. If not, then an ASP error will inform you of the foolishness of your code. By setting the Buffer property to True and clearing the Response, we satisfy this requirement. If these was a large ASP page, then I would put Response.Buffer at or near the top.

The AddHeader method is used to specify the method that the browser will use to handle the incoming file. As wel have already mentioned,the default is to launch the application on the client's system that is associated with the file type of the download. With the AddHeader method, we tell the browser to prompt to save the file locally using "FileName" as the default. The ContentType property is to provide information about the file type so that the browser can process it correctly. The default content type of "text/html" doesn't allow the BinaryWrite method to be used. Different content types would be used for Word documents (application/ms-word), JPGs (image/jpeg) or Zip files (application/zip).

You might also like...

Comments

About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.

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.

“Never trust a programmer in a suit.” - Anonymous