How to Force Remote Downloading of MP3 files

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+
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
merkheadAuthor Commented:
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:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.