Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

August 23, 2007

Stream Binary File to ASP Response

This blog post shows how to stream a binary file to the ASP Response. It could be a MS-Word document, PDF, .zip, etc. This code becomes necessary if the server-side ASP is generating a file on-the-fly for the user to download.

It uses a freeware "Chilkat FileAccess" ActiveX which can be downloaded here: Freeware ASP FileAccess ActiveX Component.

Note: There are many examples on the Web that demonstrate how to use ADODB.Stream to send binary files to the browser. However, the drawback is that you must first load the entire file into memory (on the web server) before streaming to output. This solution is better because it truly streams — reading and writing a chunk at a time.

<%
' This example demonstrates how to stream a binary file to the ASP Response.
	
' This example will use the Chilkat FileAccess ActiveX, which is a free
' utility component available at:
' http://www.chilkatsoft.com/download/ChilkatFileAccess.zip
set fac = Server.CreateObject("Chilkat.FileAccess")
	
' The Chilkat FileAccess ActiveX can stream a binary file to the ASP Response output:
	
' Open the file:
' The integer arguments to FileOpen are identical to the arguments used
' by the Microsoft CreateFile Platform SDK function
' (http://msdn2.microsoft.com/en-us/library/aa363858.aspx)
	
' Access Modes:
' GENERIC_READ	(0x80000000)
' GENERIC_WRITE (0x40000000)
accessMode = &H80000000
	
' Share Modes:
' FILE_SHARE_READ(0x00000001)
' FILE_SHARE_WRITE(0x00000002)
shareMode = 1
	
' Create Dispositions
' CREATE_NEW          1
' CREATE_ALWAYS       2
' OPEN_EXISTING       3
' OPEN_ALWAYS         4
' TRUNCATE_EXISTING   5
createDisp = 3
	
' Attributes:
' FILE_ATTRIBUTE_READONLY         0x00000001
' FILE_ATTRIBUTE_HIDDEN           0x00000002
' FILE_ATTRIBUTE_SYSTEM           0x00000004
' FILE_ATTRIBUTE_DIRECTORY        0x00000010
' FILE_ATTRIBUTE_ARCHIVE          0x00000020
' FILE_ATTRIBUTE_NORMAL           0x00000080
' FILE_ATTRIBUTE_TEMPORARY	   0x00000100
fileAttr = 0
	
fileName = Server.MapPath("/data/abc123.zip")
success = fac.FileOpen(fileName,accessMode,shareMode,createDisp,fileAttr)
if (success = 0) then
	Response.Write fac.LastErrorHtml
	Response.End
end if
	
' Send the response header.
' The filename that the end-user will see is specified in the Content-Disposition header.
' The Response.ContentType should be set according to the type of file.
' (see the list of content types below)
Response.Buffer = True
Response.Expires = 0
Response.ContentType = "application/zip"
Response.AddHeader "Content-transfer-encoding", "binary"
Response.AddHeader "Content-Disposition", "attachment;filename=test.zip"
	
' Stream the file to the output:
Dim dataChunk
do while (fac.EndOfFile = 0)
	' Read chunks of 4K at a time...
	dataChunk = fac.FileRead(4096)
	if (not (isNull(dataChunk))) then
		Response.BinaryWrite(dataChunk)
		Response.Flush()
	end if
loop
	
fac.FileClose
	
Response.Flush()
	
%>

Here is a list of common content-types according to filename extension:

.abs: audio/x-mpeg
.ai: application/postscript
.aif: audio/x-aiff
.aifc: audio/x-aiff
.aiff: audio/x-aiff
.aim: application/x-aim
.art: image/x-jg
.wmv: video/x-ms-wmv
.asf: video/x-ms-asf
.asx: video/x-ms-asf
.au: audio/basic
.avi: video/x-msvideo
.avx: video/x-rad-screenplay
.bcpio: application/x-bcpio
.bin: application/octet-stream
.bmp: image/bmp
.body: text/html
.cdf: application/x-cdf
.cer: application/x-x509-ca-cert
.class: application/java
.cpio: application/x-cpio
.csh: application/x-csh
.css: text/css
.dib: image/bmp
.doc: application/msword
.dtd: text/plain
.dv: video/x-dv
.dvi: application/x-dvi
.eps: application/postscript
.etx: text/x-setext
.exe: application/octet-stream
.gif: image/gif
.gtar: application/x-gtar
.gz: application/x-gzip
.hdf: application/x-hdf
.hqx: application/mac-binhex40
.htc: text/x-component
.htm: text/html
.html: text/html
.hqx: application/mac-binhex40
.ief: image/ief
.jad: text/vnd.sun.j2me.app-descriptor
.jar: application/java-archive
.java: text/plain
.jnlp: application/x-java-jnlp-file
.jpe: image/jpeg
.jpeg: image/jpeg
.jpg: image/jpeg
.js: text/javascript
.kar: audio/x-midi
.latex: application/x-latex
.m3u: audio/x-mpegurl
.mac: image/x-macpaint
.man: application/x-troff-man
.me: application/x-troff-me
.mid: audio/x-midi
.midi: audio/x-midi
.mif: application/x-mif
.mov: video/quicktime
.movie: video/x-sgi-movie
.mp1: audio/x-mpeg
.mp2: audio/x-mpeg
.mp3: audio/x-mpeg
.mpa: audio/x-mpeg
.mpe: video/mpeg
.mpeg: video/mpeg
.mpega: audio/x-mpeg
.mpg: video/mpeg
.mpv2: video/mpeg2
.ms: application/x-wais-source
.nc: application/x-netcdf
.oda: application/oda
.pbm: image/x-portable-bitmap
.pct: image/pict
.pdf: application/pdf
.pgm: image/x-portable-graymap
.pic: image/pict
.pict: image/pict
.pls: audio/x-scpls
.png: image/png
.pnm: image/x-portable-anymap
.pnt: image/x-macpaint
.ppm: image/x-portable-pixmap
.ps: application/postscript
.psd: image/x-photoshop
.qt: video/quicktime
.qti: image/x-quicktime
.qtif: image/x-quicktime
.ras: image/x-cmu-raster
.rgb: image/x-rgb
.rm: application/vnd.rn-realmedia
.roff: application/x-troff
.rtf: application/rtf
.rtx: text/richtext
.sh: application/x-sh
.shar: application/x-shar
.smf: audio/x-midi
.snd: audio/basic
.src: application/x-wais-source
.sv4cpio: application/x-sv4cpio
.sv4crc: application/x-sv4crc
.swf: application/x-shockwave-flash
.t: application/x-troff
.tar: application/x-tar
.tcl: application/x-tcl
.tex: application/x-tex
.texi: application/x-texinfo
.texinfo: application/x-texinfo
.tif: image/tiff
.tiff: image/tiff
.tr: application/x-troff
.tsv: text/tab-separated-values
.txt: text/plain
.ulw: audio/basic
.ustar: application/x-ustar
.xbm: image/x-xbitmap
.xpm: image/x-xpixmap
.xwd: image/x-xwindowdump
.wav: audio/x-wav
.wbmp: image/vnd.wap.wbmp
.wml: text/vnd.wap.wml
.wmlc: application/vnd.wap.wmlc
.wmls: text/vnd.wap.wmls
.wmlscriptc: application/vnd.wap.wmlscriptc
.wrl: x-world/x-vrml
.Z: application/x-compress
.z: application/x-compress
.zip: application/zip
.xml: text/xml


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.