?
Solved

How to create a link to download an MP3 file (NOT stream it in the browser)

Posted on 2011-03-18
20
Medium Priority
?
1,301 Views
Last Modified: 2012-05-11
I'd like to have a link on my page that allows for an MP3 download.  It seems that by default it opens the MP3 and plays it in the browser rather than prompting the user to save or open.  I found a PHP script that I can pass it to which will force this, but is there a way to do it straight up in Coldfusion or HTML or maybe even Javascript?

0
Comment
Question by:benjaminfam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +3
20 Comments
 
LVL 12

Expert Comment

by:Amick
ID: 35164949
This is actually determined at the browser rather than at the server.
0
 
LVL 6

Expert Comment

by:Tomislavj
ID: 35164987
use FTP not HTTP as the source link, this should bring up the save / open dialog box?
0
 

Author Comment

by:benjaminfam
ID: 35165088
Unfortunately If I use FTP, the user is prompted for username/password.
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 12

Expert Comment

by:Amick
ID: 35165778
You could use this type of link:
ftp://anonymous:password@host:port/path
after configuring your directory for anonymous ftp access.

That still doesn't change the fact that some browsers such as Firefox and IE, when configured to autoplay mp3s, will autoplay mp3s just as they've been configured to do.

0
 
LVL 16

Expert Comment

by:AlexPace
ID: 35165871
What if you make it a zip file?
0
 
LVL 6

Expert Comment

by:Tomislavj
ID: 35166524
you can try to change MIME TYPE to application/force-download or application/x-msdownload for MP3 files, or something that will work for you
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35167560
In CF you can force download by using CFHEADER and CFCONTENT Togeather

change the path's...

<cfheader name="Content-Length" value="#getfile.size#">
<cfheader name="Content-Type" value="audio/mpeg">
<cfheader name="Content-Disposition" value="attachment;filename=#url.file#">
<cfheader name="Content-Location" value="#URLEncodedFormat(url.file)#">

<cfcontent type="application/octet-stream" file="#session.securefiles#\#url.file#">

Open in new window

0
 

Author Comment

by:benjaminfam
ID: 35167609
I think that using the cfheader / cfcontent combo appears to be the right idea.  Would these commands go in a separate file all by themseves, which would be called by my page that has the download link in it?  For example, would this code be the entire contents of a file such as download.cfm which would then be linked to from elsewhere with a link such as <a href=download.cfm?file=[name of file to download] ?

Just trying to understand this fully.  Thanks much.

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35167683
Yes, you can place them in download.cfm and then you can link to it.. <a href=download.cfm?file=[name of file to download].

you can also have them in the same file as well, just call the same file and add a flag, say filename.mp3&download=1, then put this code in cfif tags, which would say <cfif download> .... </cfif>...


0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35178578
You can do it the Following way!

Create a New File, DownloadSpecs.cfm and inside that downloadSpecs.cfm file use the following Tutorial to Create It!

http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/force-file-download-27

Now Suppose you have an Page which is MyPage.cfm

Now in MyPage.cfm, you have the following link as:

<a href="downloadSpecs.cfm?myfile=#filename#" target="_blank">

Now what will happen, it will go to the downloadSpecs.cfm page and depending upon what type of file it is, it will force a download, please note you can amend that page to include any additional mime type

There you go, Now do whatever you want to do!

Suppose you have different folders for differentmime types, then you can amend the url to pass the path also with the filename so you can do multiple things witha single phase
0
 

Author Comment

by:benjaminfam
ID: 35181206
I made this work with the following code:

<cfset variables.filepath = ExpandPath('mediauploads\') & URL.myfile />
<cfset variables.contentType = "audio/mpeg" />
<cfheader name="Content-disposition" value="attachment;filename=#URL.myfile#" />
<cfheader name="Content-Type" value="#variables.contentType#">

However, a couple of you had the following line as part of it:

<cfcontent type="#variables.contentType#" file="#variables.filepath#" />

If I add this line, my page bombs out with a 500 Internal server error.   Both of the variables referenced in that line are legitimate, but it causes an error.  At the same time, though, things function OK with this line of code omitted.  Should I be concerned?  What is that line supposed to actually do?

See for yourself at http://pbu.edu/media/index.cfm.

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35183593
from Docs

CFCONTENT

Sets the MIME content encoding header for the current page; if the encoding information includes a character encoding, sets the character encoding of generated output.
Sends the contents of a file, or of a variable that contains binary data, as the page output.


http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_11.html

Example usage of tag on DOCS

http://livedocs.adobe.com/coldfusion/8/htmldocs/manageFiles_8.html
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35183669
Try this..

<cfset variables.filepath = ExpandPath('mediauploads\') & URL.myfile />
<cfset variables.contentType = "audio/mpeg" />
<cfheader name="Content-disposition" value="attachment;filename=#URL.myfile#" />
<cfcontent type="#variables.contentType#" file="#variables.filepath#" />

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35183954
A Simple way to do this!

Page.cfm

<a href="downloadSpecs.cfm?myfile=#url.myfile#&mime=audio">

on the page downloadSpecs.cfm

<cfparam name="url.mime" default="">

<cfset variables.filepath = ExpandPath('mediauploads\') & URL.myfile />

<cfif url.mime eq 'audio'>
<cfset variables.contentType = "audio/mpeg" />
<cfelse>
<cfset variables.contentType = "application/unknown" />
</cfif>

<cfheader name="content-disposition" value="attachment;filename=#variables.filepath#">
<cfcontent type="#variables.contentType#" file="#variables.filepath#">

That's all it will work
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35183967
this

<a href="downloadSpecs.cfm?myfile=#url.myfile#&mime=audio">


as

<a href="downloadSpecs.cfm?myfile=#yourmp3filename#&mime=audio">
0
 

Assisted Solution

by:benjaminfam
benjaminfam earned 0 total points
ID: 35184362
Well, like I said in my last post, I actually have this working with the following code:

<cfset variables.filepath = ExpandPath('mediauploads\') & URL.myfile />
<cfset variables.contentType = "audio/mpeg" />
<cfheader name="Content-disposition" value="attachment;filename=#URL.myfile#" />
<cfheader name="Content-Type" value="#variables.contentType#">

However, if I add the <cfcontent> line as follows, I get the 500 error.

<cfcontent type="#variables.contentType#" file="#variables.filepath#" />

Again, it works.  Just not with the <cfcontent> line.  Unless there's some sort of reason why it should be there, I can just accept that it works without it and doesn't work with it.

See for yourself at http://pbu.edu/media/index.cfm.
0
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 1000 total points
ID: 35186179
You are specifying the content-type in cfheader itself, so that if fine.

what's beeing suggeted is to specify the content-disposition tag in cfheader and then use cfcontent to specify the type and the file.
0
 
LVL 16

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 1000 total points
ID: 35186475
ok! Check with your host if they had allowed the cfcontent Tag, Can you paste the 500 full code what is there, so we can hrlp you debug furthur!

The way youre doing might be OK, but the best way is cfcontent, just check it and if you can paste the image here, that will be nice of the 500 error code!
0
 

Author Comment

by:benjaminfam
ID: 35190106
Thank you everyone for your help with this.  Since it is functional right now, I'm going to accept it as is and stick with it.   I very much appreciate all of the feedback and suggestions.
0
 

Author Closing Comment

by:benjaminfam
ID: 35225304
Thank you everyone for your help with this.  Since it is functional right now, I'm going to accept it as is and stick with it.   I very much appreciate all of the feedback and suggestions.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

741 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