Chr*_*ris 7 ruby ruby-on-rails
我想创建一个Rails控制器,从Web下载一系列jpg文件,并直接将它们作为二进制文件写入数据库(我不是要尝试上传表单)
在做这件事的路上有任何线索吗?
谢谢
编辑:这是我使用attachment-fu gem编写的一些代码:
http = Net::HTTP.new('awebsite', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() { |http|
req = Net::HTTP::Get.new("image.jpg")
req.basic_auth login, password
response = http.request(req)
attachment = Attachment.new(:uploaded_data => response.body)
attachement.save
}
Run Code Online (Sandbox Code Playgroud)
我得到一个"未定义的方法`content_type'为#"错误
使用open-url(在Ruby stdlib中)获取文件,然后使用像paperclip这样的gem 将它们作为模型的附件存储在db中.
更新:
Attachment_fu不接受原始字节,它需要一个"类文件"对象.使用LocalFile的这个示例以及下面的代码将图像转储到临时文件中,然后将其发送到您的模型.
http = Net::HTTP.new('www.google.com')
http.start() { |http|
req = Net::HTTP::Get.new("/intl/en_ALL/images/srpr/logo1w.png")
response = http.request(req)
tempfile = Tempfile.new('logo1w.png')
File.open(tempfile.path,'w') do |f|
f.write response.body
end
attachment = Attachment.new(:uploaded_data => LocalFile.new(tempfile.path))
attachement.save
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9476 次 |
| 最近记录: |