使用大小小于10kb的开放URI和回形针存储图像 [英] Storing image using open URI and paperclip having size less than 10kb

查看:157
本文介绍了使用大小小于10kb的开放URI和回形针存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从旧网站导入一些图标。这些图标的大小小于10kb。因此,当我尝试将图标导入其返回的stringio.txt文件时。

I want to import some icons from my old site. The size of those icons is less than 10kb. So when I am trying to import the icons its returning stringio.txt file.

require "open-uri"
class Category < ActiveRecord::Base
   has_attached_file :icon,  :path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"
  def icon_from_url(url)
    self.icon = open(url)
   end    
end

在佣金任务中。

   category = Category.new
   category.icon_from_url "https://xyz.com/images/dog.png"
   category.save


推荐答案

尝试:

def icon_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.icon = file
end

这篇关于使用大小小于10kb的开放URI和回形针存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆