在rails应用程序中设置下载文件的文件名 [英] setting the filename for a downloaded file in a rails application

查看:126
本文介绍了在rails应用程序中设置下载文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器操作,允许用户下载一个扩展名为.ppt的文件。它不是一个真正的powerpoint二进制,只是一个xml-ish格式,powerpoint可以读取。该文件是从名为ElementsController的控制器的show操作下载的,但show操作实际上未在控制器中定义,但是,在app / views / elements / show.ppt.builder中有一个模板文件。我有Mime ::类型注册在config / initializers / mime_types.rb如此:

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.registermultipart / related ,:ppt

并且文件正确下载,并在Windows系统上使用powerpoint打开,但问题是文件名。文件的名称为3.ppt,其中三是url中的id参数。我想知道是否有一种方法来设置文件名比3.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

推荐答案

您可以使用 send_data

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

这个的另一个好处是可以使用x-sendfile,

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

/elements/3/files/foo.ppt

然后在FilesController的show方法中你可以发送任何id参数。

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

这篇关于在rails应用程序中设置下载文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆