2011-12-01 107 views

回答

0

上傳文件到S3時文件名必須有沒有空格或特殊字符
要與空間使用以下

yourmodel.rb

class Video < ActiveRecord::Base 
    has_attached_file :video, 
    :path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name", 
    :url => "/system/:attachment/:id/:style/:normalized_video_file_name" 

    Paperclip.interpolates :normalized_video_file_name do |attachment, style| 
    attachment.instance.normalized_video_file_name 
    end 

    def normalized_video_file_name 
    "#{self.id}-#{self.video_file_name.gsub(/[^a-zA-Z0-9_\.]/, '_')}" 
    end 
end 

什麼是我們在這裏做上傳文件?很簡單,在has_attached_file中,我們編輯paperclip默認返回路徑和url的方式,這是保存和加載文件以顯示它時最相關的組件。回形針默認值是:前面有

path default => ":rails_root/public/system/:attachment/:id/:style/:filename" 
url default => "/system/:attachment/:id/:style/:filename" 


價值觀 ':' 是標準的插值回形針有
http://blog.wyeworks.com/2009/7/13/paperclip-file-rename

0

你需要一個補充:s3_headers進入您的has_attachment行:

has_attached_file :asset, 
:storage => :s3, 
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
:path => "uploads/:id/:basename.:extension", 
:s3_headers => {"Content-Disposition" => "attachment"}, 
:s3_permissions => 'authenticated-read', 
:s3_protocol => "http", 
:bucket => "my_bucket_or_something"