I’m looking for a user to click a link to download an Mp3 file, but each
way I try it loads the file in the browser to play.
How can I force a download instead of playing directly in the browser?
Thanks.
I’m looking for a user to click a link to download an Mp3 file, but each
way I try it loads the file in the browser to play.
How can I force a download instead of playing directly in the browser?
Thanks.
This won’t be a popular answer, but you could map the mp3 mime-type on
your
server to application/octet-stream which the browser will not try to
handle
but it will prompt the open/save dialog.
On 2/17/07, Bill [email protected] wrote:
Posted via http://www.ruby-forum.com/.
–
Thanks,
-Steve
http://www.stevelongdo.com
On Feb 17, 2007, at 7:03 PM, Bill wrote:
I’m looking for a user to click a link to download an Mp3 file, but
each
way I try it loads the file in the browser to play.How can I force a download instead of playing directly in the browser?
Add a Content-Disposition header:
Content-Disposition: attachment; filename=“foo.mp3”
-pete
Add a Content-Disposition header:
Content-Disposition: attachment; filename=“foo.mp3”
-pete
Check out the send_file method, this will do it for you.
You then create a controller to handle the presentation of the file.
Below is an example, if its an image render inline, if not, then force
a download. I’m using this with the attachment_fu plugin, which gives
me methods like image? to show whether or not its an image. But, you
could do something else. Basically, you send the full path to the
file to the send_file method. You should then send the correct
content-type and specify that the disposition is ‘attachment’
class ViewFileController < ApplicationController
def show
@asset = Asset.find(params[:id])
if @asset.image?
inline
else
attachment
end
rescue ActiveRecord::RecordNotFound
render :text => “return a file not found error here”
end
private
def attachment
send_file(@asset.full_filename,
:filename => @asset.filename,
:type => @asset.content_type,
:disposition => ‘attachment’,
:streaming => true,
:buffer_size => 4096)
end
def inline
send_file(@asset.full_filename,
:filename => @asset.filename,
:type => @asset.content_type,
:disposition => ‘inline’)
end
end
Why not give the user the option of both, though? Right-clicking to
download
or clicking to listen? I frequently middle-click to open mp3s in a
background tab to listen to podcasts, music, etc. Instead of forcing
your
user to do things one way I think they’d appreciate having a choice.
RSL
Hi,
In your controller, you can use
send_data @mp3filecontent, :type => “put correct mime type
here”, :disposition => “attachment”
Hope this helps,
Regards,
Oyku.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs