How to Force Remote Downloading of MP3 files

Posted on 2004-11-22
Medium Priority
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: http://www.sherriberger.com/multimedia/bergercommercial.mp3.

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

Expert Comment

ID: 12646173
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

ID: 12646570
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:

Accepted Solution

chisholmd earned 2000 total points
ID: 12646833
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")
inet.open "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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

864 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