Downloading any file using ASP, FSO and the ADODB Stream object
In this article, we will see how to allow a user to download any file from our web server. They will see a prompt, giving them the option of opening or saving it, rather than simply opening it which is the default. We can achieve this using the FSO and ADODB objects.
Published: May 8, 2002
| Last Edited: Aug 21, 2005
Tested with: ASP 3.0
Category: ASP
100,834 views
Introduction
In this article, we will see how to allow a user to download any file from our web server. They will see a prompt, giving them the option of opening or saving it, rather than simply opening it which is the default. We can achieve this using the FSO and ADODB objects.
The desired effect
There are times when you want users to download a file instead of opening it up in a browser; like images, text files, ASP code files, MS Office files like Powerpoint or Word files, etc. That's easy to do for a ZIP file for example, but hard with a GIF or TXT. The reason is that anything that the browser recognizes as a valid format, it will open right away without giving you the option to save it. We want the user to receive a prompt though, asking them if they want to save or open the file, like below:

We can achieve the above for any file, by simply editing the Response that the browser receives from our web server. By editing the header and the content type, we can prepare the browser to accept binary streams, which would then save as attachments. So, it will always prompt the users to save the content.
ASP Code
Copy the code above and save it as download.asp on your web server.
To use this code, you link to the page, passing the location of the file
you want to send to the user. For example:
<a href="download.asp?file=/images/xefteri.gif">
The first part of the code does some basic error checking. The sub DownloadFile checks to see if the file is there, and if it is then it sends it as a binary stream using the ADODB Stream object. The header Content-Length is set so that the browser can properly display the progress bar.
Possible issues
If you are trying to stream a file bigger than 4MB, then IIS 6 might send back an error saying something to the sort of "Response Buffer Limit Exceeded". This is a maximum file setting in the web server's metabase, which you can easily fix.
Conclusion
Make sure that you have MDAC 2.5+ installed in order for this to work. It would be very easy to add some security to this code. For example, you could combine your database of content access to it. You would then first check if the users have access to content and if they do, only then proceed to send the file to them.