Force Download Dialog box with ASP

Force Download Dialog box with ASP
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 https://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.

https://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.




RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 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 Lisa Shea for details.