2016-05-08 96 views
0

我試圖使用在軌道上回形針保存Base64編碼字符串,但生成的文件沒有擴展回形針:保存Base64編碼的文件,保存沒有擴展

編碼字符串如下:

{"model"=>{"photo"=>"data:image/jpeg;base64,/9j..} 

回形針配置

has_attached_file :photo, 
        :styles => { medium: "380x380>", small: "200x200>", thumb: "100x100>" }, 
       :path => ":rails_root/model/:style/:id.:extension", 
       :url => "/object_image/model/:style/:id.:extension",      
        :default_url => "/images/default-avatar.png" 

validates_attachment_content_type :photo, 
            :content_type => ["image/jpg", "image/jpeg", 
                "image/png", "image/gif"] 

生成的文件是:1.

任何幫助是appeciated。 謝謝

回答

2

嘗試使用圖像的內容類型

tempfile = self.image.queued_for_write[:original] 
unless tempfile.nil? 
    extension = File.extname(tempfile.original_filename) 
    if !extension || extension == '' 
    mime = tempfile.content_type 
    ext = Rack::Mime::MIME_TYPES.invert[mime] 
    self.image.instance_write :file_name, "#{tempfile.original_filename}#{ext}" 
    end 
end 

特別感謝StackOverflow的設置回擴展。