views:

363

answers:

2

Hi All,

I have a controller action that allows a user to download a file with an extension of .ppt . It's not really a powerpoint binary, just an xml-ish format that powerpoint can read. the file is downloaded from the show action of a controller called ElementsController, but the show action is not actually defined in the controller, there is, however, a template file for it in app/views/elements/show.ppt.builder . I have the Mime::Type registered in config/initializers/mime_types.rb as such:

Mime::Type.register "multipart/related", :ppt

and the file downloads properly, and opens with powerpoint on a windows system, yet the problem is the filename. the name of the file is 3.ppt where three is the id parameter in the url. I would like to know if there is a way to set the filename to something a little more descriptive than 3.ppt.

thx,

-C

+2  A: 

You could use send_data:

send_data pptdata, :filename => 'your_file_name.ppt', 
   :disposition => 'inline', :type => "multipart/related"

Another advantage of this is you can use x-sendfile, so that you're mongrel/thin isn't waiting while the client downloads the data.


Another option would be to have a route like:

/elements/3/files/foo.ppt

Then in your show method for the FilesController you can send whatever the id parameter would be.

jonnii
the problem with send_data is that i would have to open up the method in the controller and define respond_to do |format|, which is what i'm trying to avoid.
Chris Drappier
What's wrong with respond_to?
jonnii
I updated my question with another idea.
jonnii
it just seems like there should be no reason to actually set respond_to in your individual controller actions, because rails does it automatically for you if you follow naming conventions and register your mime-types. I only want to set the filename, rather than redefine the whole action.
Chris Drappier
I guess the thing is you're requesting a URI with a format. I would expect it to return a document with that name. Did you see my other suggestion?
jonnii
A: 

A possible example:

def show
    @item = Item.find(params[:id])
    respond_to do |format|
        format.html # show.html.erb
        format.ppt {
            response.headers['Content-Disposition'] = "attachment; filename=\"#{@item.filename}.ppt\""
        } # show.ppt.erb
        format.xml  { render :xml => @item }
    end
end
Grant Neufeld