Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

controller - setting the filename for a downloaded file in a rails application

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...