How to send a dynamically generated file in a rails app

邮差的信 提交于 2019-12-03 15:03:18

If it's a simple problem as you describe it, a simple solution like that will do. Just don't forget the :filename option, otherwise the file will be named as "project_file".

  def project_file
    project = Project.find(params[:id])
    send_data project.generate_really_simply_text_file_report, :filename => "#{project.name}.txt"
  end

Edit:

your project#generate_really_simply_text_file_report should return either the binary data, the path for the file or a raw String.

  def download
    content = "chunky bacon\r\nis awesome"
    send_data content,  :filename => "bacon.txt" 
  end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!