Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rails. Download remote image as attachment in browser
# in controller
# for local files
send_file '/path/to/file', :type => 'image/jpeg', :disposition => 'attachment'
# for remote files
require 'open-uri'
url = ''
data = open(url).read
send_data data, :disposition => 'attachment', :filename=>"photo.jpg"

Valve commented Mar 6, 2014

Excellent, thank you.

jmwelch commented Feb 5, 2015

+1. Just to note, if you don't know what type of file you'll be remotely downloading ahead of time, just remove the .jpg extension from :filename!

Thanks, it's very helpful to me.

I wouldn't recommend using this with user-specified URL because of security issues with using open-uri. This article sums it up well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment