简体   繁体   中英

Ruby on Rails: Downloading files with multiple dots in filename (uploaded using Carrierwave)

The view:

<%= link_to File.basename(attachment.attachment.url), "/uploads/#{attachment.id}/#{File.basename(attachment.attachment.url)}" %>

The controller:

# ...
  def download
    path = "#{Rails.root}/uploads/"+ params[:id] + "/"+ params[:basename] +"."+ params[:extension]

    send_file path, :x_sendfile=>true
  end
# ...

The route:

match "/uploads/:id/:basename.:extension", :controller => "attachments", :action => "download", :conditions => { :method => :get }

The error is get is:

Routing Error

No route matches [GET] "/uploads/38/Screen_shot_2012-02-18_at_2.20.49_PM.png"
match "/uploads/:id/:filename.:extension", :controller => "attachments", :action => "download", :constraints => { :filename => /[^\/]+/ },  :conditions => { :method => :get }

感谢forker,我被引导到这个博客: http ://coding-journal.com/rails-3-routing-parameters-with-dots/

match "/uploads/:id/:filename", :controller => "attachments", :action => download, :requirements => { :filename => /.*/ }

通过这篇博客文章

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM