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.
Bill (Guest)
on 2007-02-18 05: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.
Steve L. (Guest)
on 2007-02-18 05: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 <removed_email_address@domain.invalid> wrote:
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Thanks,
-Steve
http://www.stevelongdo.com
peter royal (Guest)
on 2007-02-18 06: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
dusty (Guest)
on 2007-02-18 09: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
Russell N. (Guest)
on 2007-02-18 17: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
Oyku Gencay (Guest)
on 2007-02-18 17: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.