Downloading files to the browser with ADODB.Stream
page 1 of 1
Published: 25 Mar 2004
Unedited - Community Contributed
Abstract
This script demonstrates getting properties of a file, such as name and size, and using a response stream to send the file to the users browser.
by Chris Garrett
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42128/ 39

Downloading files to the browser with ADODB.Stream
Downloading files to the browser with ADODB.Stream
by Chris Garrett

This script demonstrates getting properties of a file, such as name and size, and using a response stream to send the file to the users browser.

VIEW SOURCE
/chrisg/tools/view-downloadfile.asp

GET THE SOURCE
/chrisg/tools/download.asp?file=downloadFile.asp

SEE IT IN ACTION
/chrisg/tools/download.asp?file=chrispic-small.gif

The script begins by calling the function "downloadFile" with the file we wish to download as a parameter, in this case getting it from the "file" request variable.

DownloadFile() first gets full path of specified file and checks to see if the file exists using the FileExists(strFilename) method of the filesystem object. It then gets the length of the file (the size) using the .size property of the file object. 

Lines 19 and 20 show how we create the stream object and in line 23 we set the stream to binary. Line 42 uses the loadfromfile method to begin the process to read in the file, and after the headers have been set between lines 49 and 52, the file is output using Response.Binarywrite and the contents of the streams "read".

Future improvements

I have fudged this script a bit by using a default content type of "octet", you could improve this scripts functionality by specifying the corrent mime type.



User Comments

Title: Not enough storage is available to complete this operation.   
Name: shashi
Date: 2011-10-07 5:36:22 AM
Comment:
still problem
Title: RE: Files bigger then 4mb   
Name: FesterSXS
Date: 2009-03-06 5:21:02 AM
Comment:
Thanks for this code guys - it has really helped. I have made a minor improvement as the code was unable to cope with filenames that contained spaces.

I changed this line:
Response.AddHeader "Content-Disposition", "inline; filename=" & f.name

to...

Response.AddHeader "Content-Disposition", "inline; filename=""" & f.name & ""

thanks again

Tony
Title: RE: Files bigger then 4mb   
Name: James
Date: 2009-01-05 6:17:11 AM
Comment:
Note:

The buffering code supplied by RuterB is buggy - DON'T USE IT. He is not passing anything to the adoStream.Read() method - so it's not actually splitting the file into chunks at all.

This is a better solution;

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile(sharePath & Filename)

Set adoStream = CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(f.Path)
Response.Buffer = true
Response.clear
Response.ContentType = "application/x-unknown" ' arbitrary
Response.AddHeader "Content-Length", dataSize
Response.AddHeader "Content-Disposition", "inline; filename=" &f.name

Response.flush
While not adoStream.eos
Response.BinaryWrite adoStream.Read(1024 * 8)
Response.flush
Wend
Response.End()

The "8" can be increased/decreased depending on buffer size. I hope this helps...
Title: how can we make a code to download file from website   
Name: mukesh
Date: 2008-07-08 2:34:34 AM
Comment:
plz help me its urgent
Title: Files bigger then 4mb   
Name: WateroakMike
Date: 2008-02-29 10:12:43 AM
Comment:
RuterB's code was a lot of help! But I still had trouble with large files hosted on an IIS6 server. I was getting error "Response Buffer Limit Exceeded". Since this is a shared server operated by a hosting service, I don't have the option of increasing the response buffer limit. The solution was to just reduce the amount of data read like so:

Response.BinaryWrite adoStream.Read(chunkSize)

Thank again...
Title: Files bigger then 4mb   
Name: RuterB
Date: 2007-08-01 5:02:12 AM
Comment:
You might run into trouble with files bigger then 4mb, one way to overcome is to flush the buffer before it is full.

You can also overcome the buffer limitation by flushing the buffer before it is full.

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile(sharePath & Filename)

Set adoStream = CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(f.Path)
dataSize = f.size

Response.Buffer = true
Response.clear
Response.ContentType = "application/x-unknown" ' arbitrary
Response.AddHeader "Content-Length", dataSize
Response.AddHeader "Content-Disposition", "inline; filename=" &f.name
Response.flush

dataPosition = 0
chunkSize = 4194304
do while dataPosition < dataSize
Response.BinaryWrite adoStream.Read()
Response.flush
dataPosition = dataPosition + chunkSize
loop
Title: Downloadin file   
Name: Ritesh
Date: 2007-02-10 1:43:35 AM
Comment:
Thank you
Title: Download Files   
Name: sivasankari
Date: 2006-01-18 12:16:39 AM
Comment:
Error Type:
Server.MapPath(), ASP 0171 (0x80004005)
The Path parameter must be specified for the MapPath method.

/sla/sla/downloadfile.asp, line 11

thanks in advance




Pl. help me
Title: Getting data in xml format and i want to use the data as recordset   
Name: shanker
Date: 2005-11-03 6:43:31 AM
Comment:
i'm getting the data in the xml format and i want this data to be as recordset in the asp page

for that , i'm using
str=createobject("ADODB.Stream")
but it is getting that the object is not creating

plz help me
Title: converting the data from xml to recordset   
Name: shanker
Date: 2005-11-03 6:40:50 AM
Comment:
plz help me
Title: Appreciations   
Name: Priya
Date: 2005-03-17 6:20:51 AM
Comment:
Thanks very much, your code helped me a lot!
Title: Downloading files to the browser with ADODB.Stream   
Name: shakeel
Date: 2005-02-25 2:18:44 AM
Comment:
excellent

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-02-20 3:34:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search