Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a file from a URL and save it in Rails?

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.)

like image 247
Alok Swain Avatar asked Mar 25 '10 13:03

Alok Swain


People also ask

How do I download a file in rails?

For Example: i Have my file in SVG folder inside Public Directory. Now we Can Access any file in Public Folder Like below and Pass id and Download option. Download option rename any file which u want to download. Now Click able link is Ready We Can Click on Above link to Download the File.

How do you download a file in Ruby?

Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri . open-uri extends Kernel#open so that it can open URIs as if they were files. We can use this to download an image and then save it as a file.

How do I read an image in Ruby?

You need to invoke mspaint.exe with the applicable command line flags. Do note though that I don't believe MSPaint handles JPG files. You'd need to search around google or perhaps submit another question regarding MSPaint and opening files via the command line.


1 Answers

Try this:

require 'open-uri' open('image.png', 'wb') do |file|   file << open('http://example.com/image.png').read end 
like image 132
Levi Avatar answered Sep 20 '22 05:09

Levi