如何从URL下载文件并将其保存在Rails中? [英] How can I download a file from a URL and save it in Rails?

查看:131
本文介绍了如何从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

这篇关于如何从URL下载文件并将其保存在Rails中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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