Rails中文件下载的代码

controller部分:
class DownController < ApplicationController
  def index
   send_file "public/files/"+params[:filename] unless params[:filename].blank?
  end
end

views部分:
<%= link_to "下载文件",:action=>"index",:filename=>"1.txt" %>

当然controller部分的代码也可以象这样写:
send_file "#{RAILS_ROOT}/public/files/"+params[:filename] unless params[:filename].blank?

你可能感兴趣的:(Rails)