rails 之下载

控制器

  def index
    #传给前端展示层当前的id
    @id = 6
  end

  # http://127.0.0.1:3000/admin/category_statistics/export_table?id=6
  def export_table   
    puts "@params ====> #{params} #{params["id"]}"
    id = params["id"]
    puts "q id => #{id}"
    # 发给前端下载的文件
    send_file("#{Rails.root}/public/download/11.xls",
              filename: "11.xls",
              type: "application/xls")
  end

模板

a连接

<%= link_to '导出excel', export_table_admin_category_statistics_path+"?id=#{@id}", method: "get" %>
<a href="<%= export_table_admin_category_statistics_path+"?id=#{@id}"%>">222</a>

会生成下成的连接 ,点击就会下载

http:/0.0.0.0:3000/admin/category_statistics/export_table?id=6

路由

    resources :category_statistics do
      collection do
        get :export_table
        get :export_table2
      end
    end

更多参考:
https://blog.csdn.net/qq_42922647/article/details/108168293

[Haima的博客] http://www.cnblogs.com/haima/
posted @ 2021-09-03 15:35  HaimaBlog  阅读(13)  评论(0编辑  收藏  举报