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

Ruby on-RailsRubyFile Handling

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Try this:

require 'open-uri'
open('image.png', 'wb') do |file|
  file << open('http://example.com/image.png').read
end

Solution 2 - Ruby on-Rails

An even shorter version:

require 'open-uri'
download = open('http://example.com/image.png')
IO.copy_stream(download, '~/image.png')

To keep the same filename:

IO.copy_stream(download, "~/#{download.base_uri.to_s.split('/')[-1]}")

Solution 3 - Ruby on-Rails

If you're using PaperClip, downloading from a URL is now handled automatically.

Assuming you've got something like:

class MyModel < ActiveRecord::Base
  has_attached_file :image, ...
end

On your model, just specify the image as a URL, something like this (written in deliberate longhand):

@my_model = MyModel.new
image_url = params[:image_url]
@my_model.image = URI.parse(image_url)

You'll probably want to put this in a method in your model. This will also work just fine on Heroku's temporary filesystem.

Paperclip will take it from there.

source: paperclip documentation

Solution 4 - Ruby on-Rails

I think this is the clearest way:

require 'open-uri'

File.write 'image.png', open('http://example.com/image.png').read

Solution 5 - Ruby on-Rails

Check out Net::HTTP in the standard library. The documentation provides several examples on how to download documents using HTTP.

Solution 6 - Ruby on-Rails

Possibly the simplest way:

require 'open-uri'
image_url = "https://i.imgur.com/ZWnhY9T.png"
IO.copy_stream(URI.open(image_url), 'destination.png')

Solution 7 - Ruby on-Rails

All above examples are great. In my case I just wanted to create a download link from the image from URL.

If you want to make it downloadable (to your downloads folder), you can use the following code in your controller:

require 'open-uri'
file_type = url.to_s.split(".")[-1]

send_data open(url).read, filename: "some_name.#{file_type}", type: "image/#{file_type}", disposition: "attachment"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlok SwainView Question on Stackoverflow
Solution 1 - Ruby on-RailsLeviView Answer on Stackoverflow
Solution 2 - Ruby on-RailsClemens HelmView Answer on Stackoverflow
Solution 3 - Ruby on-RailssuperluminaryView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSage RossView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPär WieslanderView Answer on Stackoverflow
Solution 6 - Ruby on-RailsstevecView Answer on Stackoverflow
Solution 7 - Ruby on-RailsDirkAVAView Answer on Stackoverflow