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.

I don't think this is a good idea, as you'll be reading all the file data into memory (in data). Depending on the file size, this becomes a problem.

hello sir,how to get perticular url from json?
$.each(files, function(key,value){

in my code how can i store the link value in avariable?

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