Solved

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

Posted on 2011-03-18
20
1,261 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
  • 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
 
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 15

Expert Comment

by:myselfrandhawa
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 15

Expert Comment

by:myselfrandhawa
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 15

Expert Comment

by:myselfrandhawa
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 250 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 15

Assisted Solution

by:myselfrandhawa
myselfrandhawa earned 250 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Animate a Title 7 34
Cannot scroll down on html page 2 26
Modify Table Width 6 13
CSS grid style change 2 13
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 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

12 Experts available now in Live!

Get 1:1 Help Now