How to Force Remote Downloading of MP3 files

Posted on 2004-11-22
Last Modified: 2012-06-27
Need to programmatically force a download of an MP3 file from a remote server to a client machine? An example of usage would be: in response to a user click while in our web application, a download will automatically initiate to the client’s machine from:

This would be similar to a client right-clicking on a link in IE and selecting ‘save target as’

In our web application, we will require downloads from both local (located on our server) and remote files (as shown above). The audio files are voiceover “demos” used in the broadcast industry.

Must use HTML, ASP or .NET or similar, our servers do not allow PHP to handle this.

Implementation must operate correctly under, IE 6+, Firefox 1+, Safari 1.2+ and Netscape 7+
Question by:merkhead
    LVL 8

    Expert Comment

    The best method would be to add a static header for the mime type in IIS:

    You can also add the header dynamically with a script.  ASP:

    Set fso = CreateObject("scripting.filesystemobject")

    If Instr(file, "/") Then
          path=Left(file, InStrRev(file, "/"))
          file=Mid(file, InStrRev(file, "/")+1)
    End If

    If fso.FileExists (Server.MapPath(path & file)) Then
          Response.ContentType = "application/octet-stream"
          Response.AddHeader "content-disposition", "attachment; filename=" & file
          Set Stream = server.CreateObject("ADODB.Stream")
          Stream.LoadFromFile Server.MapPath(path & file)
          Contents = Stream.ReadText
          Response.BinaryWrite Contents
          Set Stream = Nothing
    End If


    The dynamic method is slow a resource intensive on large files.

    Author Comment

    Thanks for you response.  This will work for a local (on server) file but our application must also initiate files located on remote servers.  I'm not sure if this is even possible to initiate..

    For example, using your code, try this and you'll see Server.MapPath() doesn't handle a remote URL:
    LVL 7

    Accepted Solution

    Something like this should work.  I modified a snippet from one of my apps that was using a post of an xml doc, I tried to remove the unnecessary parts but there might still be some syntax issues in this block that you'll have to sort out.  The basic concept is this. Your user requests your ASP page, the code on your page creates an XMLHTTP object and that object requests the file from the remote server. Once your xmlhttp objectg has received the file it turns around and streams it to the client adding the appropriate headers.

    Yes, the file would sort of by downloaded twice, once from remote server to your machine, then again from your machine to the client.  Do you have access to the remote machine? Or can you provide them with an ASP to put on it for you?

    set inet = Server.CreateObject("Msxml2.serverXMLHTTP.3.0") "GET", [URL OF MP3], false,[optional username], [optional password])
    inet.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
    Response.AddHeader "Content-Disposition", "attachment; filename=[filename]
    Response.ContentType ="application/octet-stream"
    Response.BinaryWrite inet.ResponseBody

    notes: line 3 might be specific to POST's so if it gives you a problem try changing that. Also, in my app line 7 calls a function and looks up the content type based on ext. I threw in application/octet-stream as a default but you  might want to use  audio/mpeg  or something, you can try a couple content types until it behaves teh way you want.

    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

    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    732 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

    21 Experts available now in Live!

    Get 1:1 Help Now