【问题标题】:Rails download http response/displayed siteRails 下载 http 响应/显示站点
【发布时间】:2013-08-03 21:34:03
【问题描述】:

我不想在浏览器中显示由 index.api.rsb 文件呈现的 xml 文件,而是想下载它。对我来说,这听起来很简单,但我找不到解决方案。

我在控制器方法中尝试了以下操作:

def split
   if params[:export] == "yes"
       send_file *here comes the path to xml view*, :filename => "filename", :type => :xml
   end
   respond_to ...
end

结果是 MissingFile 异常...

提前致谢

【问题讨论】:

    标签: ruby-on-rails ruby download httpresponse sendfile


    【解决方案1】:

    要强制它下载,请将:disposition => attachment 添加到您的 send_file 方法中。

    来源:Force a link to download an MP3 rather than play it?

    【讨论】:

    • 仍然有 MissingFile 异常 :(
    • 那么正如吉姆所说,您的文件路径可能不正确。
    【解决方案2】:

    请注意,send_file:disposition 默认为“附件”,因此这应该不是问题。

    如果您有 MissingFile 异常,则表示路径不正确。 send_file 需要实际文件的路径,而不是需要渲染的视图。

    对于您的情况,render_to_string 可能是您需要的。请参阅this related question。它呈现视图并返回一个字符串,而不是设置响应正文。

    def split
      if params[:export] == "yes"
        send_data(render_to_string path_to_view, filename: "object.xml", type: :xml)
      end
    end
    

    【讨论】:

    • 好的,但是有下载渲染视图的选项吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多