如何从 URL 下载文件并将其保存在 Rails 中?
问题描述:
我有一个指向要保存在本地的图像的 URL,以便我可以使用 Paperclip 为我的应用程序生成缩略图.下载和保存图像的最佳方法是什么?(我研究了 ruby 文件处理,但没有遇到任何问题.)
I have a URL to an image which i want to save locally, so that I can use Paperclip to produce a thumbnail for my application. What's the best way to download and save the image? (I looked into ruby file handling but did not come across anything.)
答
试试这个:
require 'open-uri'
open('image.png', 'wb') do |file|
file << open('http://example.com/image.png').read
end