开发者

Missing extension when save image on paperclip

开发者 https://www.devze.com 2023-03-08 18:14 出处:网络
image = PortfolioFileItem.find(107) img_source = \"http://s3.amazonaws.com/test/portfolio_file_items_final/original/1.jpg\"
 image = PortfolioFileItem.find(107)  
 img_source = "http://s3.amazonaws.com/test/portfolio_file_items_final/original/1.jpg"
 image.picture_from_url(img_source)
 image.save(false)

image save DONE but missing extensionof image. this is sa开发者_如何学编程mple image name saved: open-uri20110528-6779-fpiust-0.

Please help me solved problem. thanks


To add an extension to paperclip add this line after has_attached_file as an option

:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"

You can customize this path to fit your needs however you must have the .:extension at the end, the :extension is one of many values that can be used for interpolation.

See this blog post for more information.


If the actual file does not have an extension originally you can detect extension and add it before saving

  def before_save
    tempfile = data.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.data.instance_write :file_name, "#{tempfile.original_filename}#{ext}"
      end
    end

    true
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号