logo
g Text Version
Auto
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Society & Culture
Sports
Travel & Leisure
TV & Movies

dailyclick
Bored? Games!
Postcards
Astrology
Take a Quiz
Rate My Photo

new
Ethnic Beauty
Adolescence
Middle Eastern Culture
Yoga
Vision Issues
Paper Crafts
Comedy Movies


dailyclick
All times in EST

Autism Spectrum Disorders: 4:00 PM

Full Schedule
g
g ASP Site
Editor Wanted
BellaOnline's ASP Editor

g

Force Download Dialog box with ASP
Guest Author - Christopher Combs

Have you ever been frustrated when you get to a website and there is documents, images and other content available for download and you want to do just that, download it! Instead, when you click on a content item, it proceeds to open in your browser. I was up against this issue during a project for a medical company and they needed their PDF files to be downloaded and not opened. Well, there is a way to make it open the Save As dialog box.

For this tutorial, we will assume the files are stored locally on the server and not in a database. (That is a whole different discussion)

First, you need to know the location of your files. So let’s assume your files are located at http://yourserver.com/files/filename.ext. We will assume that the Server.MapPath is "/files".

So, at this point, let’s create the ASP file that will control the downloading of the file. Start by creating a new, blank, document using NotePad or your favorite HTML editor. Place the following ASP code:

<%
'=======================
'Define the names of your functions
'=======================
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
'=======================
'Get the actual file name from the URL that is passed to the browser
'=======================
FileName = request.querystring("filename") 'Get the name from the URL
'=======================
'GIVE AN ERROR MESSAGE IF THE URL IS EMPTY
'=======================
if FileName = "" Then
response.write "Filename Not specified."
response.end
end if
'=======================
'prevent access to certain files
'=======================
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
select case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
response.write "You cannot access these file types."
response.end
end select
'=======================
'Start the download process if all is good
'=======================
response.clear
response.contentType = "application/octet-stream"
response.addheader "content-disposition", "attachment; filename=" & FileName
set stream = server.CreateObject("ADODB.Stream")
stream.type = adTypeBinary
stream.open
stream.LoadFromFile Server.MapPath("/files") & FileName
while not stream.EOS
response.BinaryWrite Stream.Read(1024 * 64)
wend
stream.Close
Set stream = Nothing
response.Flush
response.End
%>


That is it for that one. Now, save and upload this file to your server and name it filedownloader.asp

Now let’s say the filename on your server is familytree.pdf

Then you would simply pass the following URL in your browser and the File Save as Open dialog will pop up.

http://yourserver.com/filedownloader.asp?filename=familytree.pdf

That is it! You should then see this:



Good luck and you can enhance this by using databases and other cool functions to make sure that the files you want your users to access are given the ability to be downloaded and not opened in their current web browser allowing them to "stay" on your website.

This site needs an editor - click to learn more!

RSS | Related Articles | Previous Features | Site Map

Add Force+Download+Dialog+box+with+ASP to Twitter Add Force+Download+Dialog+box+with+ASP to Facebook Add Force+Download+Dialog+box+with+ASP to MySpace Add Force+Download+Dialog+box+with+ASP to Del.icio.us Digg Force+Download+Dialog+box+with+ASP Add Force+Download+Dialog+box+with+ASP to Yahoo My Web Add Force+Download+Dialog+box+with+ASP to Google Bookmarks Add Force+Download+Dialog+box+with+ASP to Stumbleupon Add Force+Download+Dialog+box+with+ASP to Reddit


Content copyright © 2009 by Christopher Combs. All rights reserved.
This content was written by Christopher Combs. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


For FREE email updates, subscribe to the ASP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor

g features
Best uses for RSS

RSS and its future.

Using the REQUEST Object

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter

jobs
what
job title, keywords
where
city, state or zip
jobs by job search


vote
Growing a Garden
Veggies and Flowers
Veggies Only
Flowers Only
No Garden

g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2009 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor