我在控制器中创建了一个新方法来下载文件。它看起来像这样。Stored_File 是存档文件的名称,并有一个名为 stored_file 的字段,它是文件的名称。使用 Carrierwave,如果用户具有下载文件的访问/权限,则会显示 URL,然后使用 send_file 将文件发送给用户。
控制器
def download
head(:not_found) and return if (stored_file = StoredFile.find_by_id(params[:id])).nil?
case SEND_FILE_METHOD
when :apache then send_file_options[:x_sendfile] = true
when :nginx then head(:x_accel_redirect => path.gsub(Rails.root, ''), :content_type => send_file_options[:type]) and return
end
path = "/#{stored_file.stored_file}"
send_file path, :x_sendfile=>true
end
看法
<%= link_to "Download", File.basename(f.stored_file.url) %>
路线
match ":id/:basename.:extension.download", :controller => "stored_files", :action => "download", :conditions => { :method => :get }