Forum: Ruby on Rails forcing an mp3 file to be downloaded

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
651af7b03ae5347b932fb231c94bbdf6?d=identicon&s=25 Bill (Guest)
on 2007-02-18 04:03
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.
5ce55767684e0cd1727db2b8b8975640?d=identicon&s=25 Steve Longdo (Guest)
on 2007-02-18 04:13
(Received via mailing list)
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 <rails-mailing-list@andreas-s.net> wrote:
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Thanks,
-Steve
http://www.stevelongdo.com
6a12550cdf7fe9a70c57f8d8e6d9be9e?d=identicon&s=25 peter royal (Guest)
on 2007-02-18 05:36
(Received via mailing list)
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
2e8848a777f7165f80da876ab1e6e7e5?d=identicon&s=25 dusty (Guest)
on 2007-02-18 08:29
(Received via mailing list)
> 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

  # GET /assets/1
  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
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-02-18 16:36
(Received via mailing list)
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
8589dbd737a28d39bb1dcd5251262e3b?d=identicon&s=25 Oyku Gencay (Guest)
on 2007-02-18 16:54
(Received via mailing list)
Hi,

In your controller, you can use

send_data @mp3filecontent, :type => "put correct mime type
here", :disposition => "attachment"

Hope this helps,

Regards,
Oyku.
This topic is locked and can not be replied to.