我正在使用Paperclip 4.2 + Rails 4.1.6以及以下型号:
class Post < ActiveRecord::Base
has_attached_file :featured_image, styles: { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment :featured_image, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] }
def featured_image_from_url(url)
self.featured_image = URI.parse(url)
end
end
当我在表单中上传文件上传器的文件时,一切正常。设置附件并生成缩略图。
但是,如果我尝试使用指向jpeg图像的远程URL,如指定的here,则无法保存帖子,因为附件的内容类型错误:featured_image_content_type:“binary / octet-stream”
如果我通过手动设置强制内容类型:
post.featured_image_content_type = "image/jpeg"
post.save
然后成功保存模型。
答案 0 :(得分:1)
这是一个宝石,可以帮助您下载到Tempfile
的网址,这样您就可以解决s3发送流mime类型https://github.com/equivalent/pull_tempfile
答案 1 :(得分:0)
您好我不知道您是否找到了解决方法。我已使用以下代码(为您的示例修改)停止Paperclip(4.2.1)在Web服务器返回不正确的内容类型时引发异常:
def featured_image_from_url(url)
self.featured_image = URI.parse(url)
# deal with the case where the webserver
# returns an incorrect content-type
adapter = Paperclip.io_adapters.for(featured_image)
self.featured_image_content_type = Paperclip::ContentTypeDetector.new(adapter.path).detect
end
可能有更整洁或更正确的方式!