How to create a download link

Posted on 2012-09-11
Last Modified: 2012-09-21
Classic ASP VBScript


I need assistance creating a straight download link in Classic ASP without having it try and play in the browser.

Just want it to prompt to "Save As".

Question by:lshane
    LVL 13

    Expert Comment

    Make a new asp page that will be the 'downloader'

    have code like this:

    Response.ContentType = "application/octet-stream"
    Response.AddHeader "Content-Disposition", "attachment; filename=test.txt"
    Response.Write("Hello World")

    In this example, I just used text file as an example. If you need to force download like jpeg, you read the image binary file and then use Response.BinaryWrite() method to write binary data to the response stream.

    Author Comment

    Thank you, themrrobert.

    The MP3 files are from relative paths stored in my Access database.

    How can I extract just the MP3 file name from the relative path?

    Author Comment

    Here is a link from a site I found from another site of a friend of mine.  (He's not a developer, or I would ask him).  lol

    <a href="downloadmanager.asp?file=Harvest08-19-12cd.mp3&force=true"><img src="images/template/download.jpg" alt="Download" border="0" /></a>

    How could I make this work with ASP?

    The mp3 file needs to be dynamic.

    The "downloadmanager.asp" file is completely blank.  It appears they are just pointing to it to "force" the "Save As" window.


    Here is the excerpt of my repeat region, so you can see recordset nomenclature, etc.  The download line is at the bottom..

    While ((Repeat1__numRows <> 0) AND (NOT rsSermons.EOF))
                            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <td width="15%" rowspan="4" align="center" valign="middle" class="sermon-image"><img src="<%=rsSermons("sermonImage")%>" alt="speaker" width="100" border="0" align="absmiddle" /></td>
                                  <td width="39%" nowrap="nowrap" class="sermon-title"><%=(rsSermons.Fields.Item("sermonTitle").Value)%></td>
                                  <td width="7%" valign="middle">&nbsp;</td>
                                  <td width="39%" valign="middle"><p id="audioplayer_<%=sermoncount%>"><a href="" class="audiotitle-middle">Requires Flash - Get it now!</a></p>
                                  <script type="text/javascript">  
                        AudioPlayer.embed("audioplayer_<%=sermoncount%>", {soundFile: "<%=(rsSermons.Fields.Item("sermonAudioPath").Value)%>",
                        titles: "<%=(rsSermons.Fields.Item("sermonTitle").Value)%>",  
                        artists: "<%=(rsSermons.Fields.Item("sermonSpeakerSalutation").Value)%> <%=(rsSermons.Fields.Item("sermonSpeakerFirst").Value)%> <%=(rsSermons.Fields.Item("sermonSpeakerLast").Value)%>",  
                        autostart: "no"  
                            </script>                                </td>
                                  <td nowrap="nowrap" class="sermon-speaker"><%=(rsSermons.Fields.Item("sermonSpeakerSalutation").Value)%><em>&nbsp;</em><%=(rsSermons.Fields.Item("sermonSpeakerFirst").Value)%><em>&nbsp;</em><%=(rsSermons.Fields.Item("sermonSpeakerLast").Value)%></td>
                                  <td nowrap="nowrap" class="sermon-date">(<%=(rsSermons.Fields.Item("sermonDate").Value)%> - <%=(rsSermons.Fields.Item("sermonAmPm").Value)%>)</td>
    <td nowrap="nowrap"><a href="downloadmanager.asp?file=Whatsyourname.mp3&force=true">Download</a>  

    The "whatsyourname.mp3" is what needs to be dynamic, but I can't make it work.

    Please help,
    LVL 6

    Accepted Solution

    Use this code and save to dwfile.asp on your directory

    ' Utilisation dans la pages
    '	appel direct : /dwdFile.asp?DwdFile=chemin/du/fichier/fichier.ext
    '   lien hypertexte : <a href="dwdFile.asp?DwdFile=chemin/du/fichier/fichier.ext" target="_self">
    Dim sFilePath, sFileName, sFileUrl, iPosStart
    sFileUrl = Request("DwdFile")
    iPosStart = InStrRev(sFileUrl, "/")
    sFileName = Right(sFileUrl, len(sFileUrl)-iPosStart)
    'Lecture du fichier
    Dim fso, oStream, sFileStream
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(Server.MapPath(sFileUrl)) Then
    	Set oStream = CreateObject("ADODB.Stream")
    	oStream.type = 1
    	oStream.LoadFromFile Server.MapPath(sFileUrl)
    	sFileStream = oStream.Read
    	Set oStream = Nothing
    	response.write "The file, named <i>"& sFileName &"</i>, doesn't exits!"
    end if
    Set fso = Nothing
    ' Préparation des entêtes HTTP pour l'envoi de fichier
    Response.AddHeader "Content-Type", "application/octetstream"
    Response.AddHeader "Content-Disposition", "attachment; filename="""& sFileName &""""
    Response.AddHeader "Cache-Control", "must-revalidate, post-check=0, pre-check=0"
    Response.AddHeader "Pragma", "no-cache"
    Response.AddHeader "Expires", 0
    'Transfer du fichier en flux binaire
    Response.BinaryWrite sFileStream

    Open in new window

    and in your code

    <a href="dwdfile.asp?dwdfile=Whatsyourname.mp3" target="_self">Download</a>  

    Open in new window


    Author Comment

    Hi, manumd.


    Thank you!

    Author Comment

    Hi, manumd.

    Suddenly something does not seem to be working properly.

    Could you, please, join me in the current open thread:


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now