paperclip upload from url

有时候我们的程序中需要远程获取并保存图片, 根据url去保存也很简单:

has_attached_file :asset , :styles => { :thumb => "100x100>" }
def asset_from_url(url)
   extname = File .extname(url)
   basename = File .basename(url, extname)
   file = Tempfile. new ([basename, extname])
   file.binmode
   open( URI .parse(url)) do |data|
     file.write data.read
   end
   file.rewind
   self .asset = file
end

使用:

image = Image.new
image.asset_from_url(url)
image.save



其他详情 : http://trevorturk.wordpress.com/2008/12/11/easy-upload-via-url-with-paperclip/

你可能感兴趣的:(image,upload,File,url,styles)